标签:
MainActivity.class
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button bt1; private Button bt2; private Button bt3; private Button bt4; private Button bt5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt1 = (Button) findViewById(R.id.bt1); bt1.setOnClickListener(this); bt2 = (Button) findViewById(R.id.bt2); bt2.setOnClickListener(this); bt3 = (Button) findViewById(R.id.bt3); bt3.setOnClickListener(this); bt4 = (Button) findViewById(R.id.bt4); bt4.setOnClickListener(this); bt5 = (Button) findViewById(R.id.bt5); bt5.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ //确认对话框 case R.id.bt1:{ Log.d("xys", "进入了"); //创建Builder构建器 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("确认对话框!");//设置标题 builder.setIcon(R.mipmap.ic_launcher);//设置图标 builder.setMessage("确认对话框内容。");//设置文本内容 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "这里是取消按钮!", Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "这里是确定 按钮!", Toast.LENGTH_SHORT).show(); } }); //创建出dialog并show出来 // AlertDialog dialog = builder.create(); // dialog.show(); builder.create().show(); break; } //单选按钮对话框 case R.id.bt2:{ final String[] list = {"男","女","女博士","程序员"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("选择性别");//设置标题 builder.setIcon(R.mipmap.ic_launcher);//设置图标 //数据源,默认选中的项目 builder.setSingleChoiceItems(list, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择的是" + list[which] + "这一项", Toast.LENGTH_SHORT).show(); } }); builder.create().show(); break; } //多选按钮对话框 case R.id.bt3:{ final String[] list = {"口琴","吉他","电脑","跳水"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("爱好"); builder.setIcon(R.mipmap.ic_launcher); builder.setMultiChoiceItems(list, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { Toast.makeText(MainActivity.this, "我的爱好是" + list[which], Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "关我屁事", Toast.LENGTH_SHORT).show(); } } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); break; } //列表对话框 case R.id.bt4:{ final String [] list ={"宣传","策划","打酱油","捣糨糊"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("爱好"); builder.setIcon(R.mipmap.ic_launcher); builder.setItems(list, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择的部门是" + list[which], Toast.LENGTH_SHORT).show(); } }); builder.create().show(); break; } //自定义对话框 case R.id.bt5:{ final String [] list ={"宣传","策划","打酱油","捣糨糊"}; //获取自定义布局 LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.dialog_layout, null); final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(view); builder.setTitle("自定义对话框"); //监听自定义按钮 final EditText text = (EditText) view.findViewById(R.id.layout_text); Button bt = (Button) view.findViewById(R.id.layout_bt1); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final String str = text.getText().toString(); Toast.makeText(MainActivity.this, "提交的内容是 " + str, Toast.LENGTH_SHORT).show(); } }); final AlertDialog dialog = builder.create(); dialog.show(); // builder.create().show(); break; } } } }
activity_main.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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/bt1" android:text="确认对话框" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/bt2" android:layout_below="@id/bt1" android:text="单选对话框" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/bt3" android:layout_below="@id/bt2" android:text="多选对话框" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/bt4" android:layout_below="@id/bt3" android:text="列表对话框" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/bt5" android:layout_below="@id/bt4" android:text="自定义对话框" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
dialog_layout.xml(自定义布局)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/layout_text" android:hint="请输入内容" android:layout_weight="5" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/layout_bt1" android:layout_weight="1.5" android:text="提交" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <ImageView android:id="@+id/layout_image" android:src="@drawable/abc" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/zmaibbs7/p/4887694.html