XML布局
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:onClick="click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/click" /> </RelativeLayout> |
Java代码
public class MainActivity extends Activity { private String[] items = new String[]{"李老师","王老师","程老师"}; private boolean[] checked = new boolean[]{true,false,true}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v){ //创建容器 AlertDialog.Builder builder = new Builder(MainActivity.this); /** * 列表 */ /*builder.setItems(items, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), items[which], 1000).show(); } });*/ /** * 单选 */ builder.setSingleChoiceItems(items, 2, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), items[which], 2000).show(); dialog.dismiss(); } }); /** * 多选 */ /*builder.setMultiChoiceItems(items, checked, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getApplicationContext(), items[which], 2000).show(); dialog.dismiss(); } });*/ //创建Dialog AlertDialog dialog = builder.create(); //显示对话框 dialog.show(); } } |