string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TestBase</string> <string name="city_name">城市</string> <string name="city_default">北京</string> <string name="btn_yesorno">是否选择提示框</string> <string name="btn_listitem">列表选择提示框</string> </resources>
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.mytest.testbase.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/city_name" /> <EditText android:id="@+id/city_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/city_default" android:inputType="text" /> <Button android:id="@+id/btn_yesorno" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="YesOrNoOnClick" android:text="@string/btn_yesorno" /> <Button android:id="@+id/btn_listitem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="ListItemOnClick" android:text="@string/btn_listitem" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_result" /> </LinearLayout>
activity.java
package com.mytest.testbase; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView textview_result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview_result = (TextView) this.findViewById(R.id.tv_result); } /** * 是否选择提示框 * * @param view */ public void YesOrNoOnClick(View view) { Builder builder = new AlertDialog.Builder(this); builder.setMessage("确认删除吗?").setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Toast.makeText(getApplicationContext(), "删除成功", // Toast.LENGTH_SHORT).show(); textview_result.setText("删除成功"); } }).setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Toast.makeText(getApplicationContext(), "删除失败", // Toast.LENGTH_SHORT).show(); textview_result.setText("删除失败"); } }); AlertDialog dialog = builder.create(); dialog.show(); } /** * 列表项选择提示框 * * @param view */ public void ListItemOnClick(View view) { Builder builder = new AlertDialog.Builder(this); final String[] cities = { "广州", "上海", "北京", "香港", "澳门" }; builder.setTitle("选择城市").setItems(cities, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { textview_result.setText("选择的城市是" + cities[which]); } }); AlertDialog dialog = builder.create(); dialog.show(); } }
时间: 2024-10-16 12:54:35