点击Activity中按钮后,会生成一个列表样式的AlertDialog对话框,对话框中的选项可以选择,这个功能可以做投票,选择器等的使用 布局代码main.xml: <?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:text="@string/hello" /> <Button android:id="@+id/myButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="100px" android:layout_y="30px" android:text="@string/str_button1" /> </AbsoluteLayout> string.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">中午去谁家蹭饭?</string> <string name="app_name">饭小统</string> <string name="str_button1">按我开始选择</string> <string name="str_alert_title">按我开始选择</string> <string name="str_alert_body">你选择的是:</string> <string name="str_ok">确认</string> <array name="items_irdc_dialog"> <item>大毛家</item> <item>二毛家</item> <item>三毛家</item> </array> </resources> java代码: import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class DialogDemo extends Activity { private Button b1; private TextView t1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); b1 = (Button)findViewById(R.id.myButton1); t1 = (TextView)findViewById(R.id.myText1); b1.setOnClickListener(myShowAlertDialog); } Button.OnClickListener myShowAlertDialog = new Button.OnClickListener(){ public void onClick(View arg0) { new AlertDialog.Builder(DialogDemo.this) .setTitle(R.string.str_alert_title)//以列表方式显示alertdialog .setItems(R.array.items_irdc_dialog, new DialogInterface.OnClickListener(){ public void onClick(DialogInterface arg0, int whichcountry) { CharSequence strDialogMsg = getString(R.string.str_alert_body); //从资源文件中得到选项数组 String[] aryShop = getResources().getStringArray(R.array.items_irdc_dialog); //创建一个alertDialog new AlertDialog.Builder(DialogDemo.this) .setMessage(strDialogMsg + aryShop[whichcountry]) .setNegativeButton(R.string.str_ok, new DialogInterface.OnClickListener(){ public void onClick(DialogInterface arg0, int arg1) { /*在这里处理要作的事*/ } }).show(); } }).setNegativeButton("取消",new DialogInterface.OnClickListener(){ public void onClick(DialogInterface d, int arg1) { d.dismiss(); //关闭取得焦点的对话框 } }).show(); } }
|
AlertDialog的嵌套--可以选择的对话框
最新推荐文章于 2024-05-29 19:59:59 发布