方法一:
1,布局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" > <TextView android:id="@+id/tv_content" android:padding="10dip" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/content" /> <Button android:id="@+id/btn_really" android:padding="10sp" android:layout_below="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/really"/> </RelativeLayout> |
2,activity_main代码
public class MainActivity extends Activity { private Button btn; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到控件 btn = (Button) findViewById(R.id.btn_really); tv = (TextView) findViewById(R.id.tv_content); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //content 当前类的this //创建对话框 AlertDialog.Builder builder = new Builder(MainActivity.this); //标题 builder.setTitle("买彩票中奖500万"); //内容 builder.setMessage("XX从此成了一个有钱人"); //图标 builder.setIcon(R.drawable.ic_launcher); //确定 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tv.setText("真的,好开心啊!!"); } }); //详细信息 builder.setNeutralButton("详细信息", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "请客吃饭!!", 2000).show(); } }); //取消 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); //创建 AlertDialog dialog = builder.create(); //显示 dialog.show(); } }); } } |
方法二:
1,布局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"> <Button android:onClick="click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/onClick"/> </RelativeLayout> |
2,activity_main代码
public class MainActivity extends Activity { private String [] items = new String []{"张三","李四","王五","赵六"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view){ //创建对话框 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], 200).show(); } }); //创建Dialog AlertDialog dialog = builder.create(); //显示 dialog.show(); } } |
方法三:自定义对话框
1,布局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" > <Button android:id="@+id/btn_dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/dialog" /> </RelativeLayout> |
2,自定义xml界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dip" android:orientation="horizontal"> <TextView android:textSize="20sp" android:id="@+id/tv_name" android:layout_width="0dip" android:layout_weight="3" android:gravity="center" android:layout_height="wrap_content" android:text="@string/name"/> <EditText android:id="@+id/et_name" android:layout_width="0dip" android:layout_marginLeft="10dip" android:layout_weight="7" android:layout_height="wrap_content" android:hint="@string/hintName"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dip" android:orientation="horizontal"> <TextView android:textSize="20sp" android:id="@+id/tv_pwd" android:layout_width="0dip" android:layout_weight="3" android:gravity="center" android:layout_height="wrap_content" android:text="@string/pwd"/> <EditText android:id="@+id/et_pwd" android:layout_width="0dip" android:layout_marginLeft="10dip" android:layout_weight="7" android:layout_height="wrap_content" android:password="true" android:hint="@string/hintPwd"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:layout_gravity="center" android:orientation="horizontal"> <Button android:id="@+id/btn_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ok"/> <Button android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cancel"/> </LinearLayout> </LinearLayout> |
3,activity_main代码
public class MainActivity extends Activity { private Button btn_dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到控件 btn_dialog = (Button) findViewById(R.id.btn_dialog); btn_dialog.setOnClickListener(new OnClickListener() { private EditText et_name; private EditText rt_pwd; private Button btn_ok; private Button btn_cancel; private AlertDialog dialog; private EditText et_pwd; @Override public void onClick(View v) { //创建容器 AlertDialog.Builder builder = new Builder(MainActivity.this); //加载标题 builder.setTitle("请注册信息"); //加载自定义的布局 View view = View.inflate(getApplicationContext(), R.layout.login, null); //找到布局中的控件 et_name = (EditText) view.findViewById(R.id.et_name); et_pwd = (EditText) view.findViewById(R.id.et_pwd); btn_ok = (Button) view.findViewById(R.id.btn_ok); btn_cancel = (Button) view.findViewById(R.id.btn_cancel); btn_ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //获取用户所输入的信息 String name = et_name.getText().toString().trim(); String pwd = et_pwd.getText().toString().trim(); //判断 if(TextUtils.isEmpty(name) && TextUtils.isEmpty(pwd)){ Toast.makeText(getApplicationContext(), "用户名和密码不能为空", 2000).show(); return; } if(name.equals("aaa") && pwd.equals("123")){ Toast.makeText(getApplicationContext(), "恭喜您,注册成功", 2000).show(); dialog.dismiss(); } } }); btn_cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); builder.setView(view); //创建对话框 dialog = builder.create(); //显示 dialog.show(); } }); } } |
本文出自 “InProvence” 博客,谢绝转载!