Android对话框的几种形式
2015-08-18 12:51
567 查看
相关参考:
http://www.oschina.net/question/54100_32486?sort=default&p=1#answers
工程结构图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/bee5d8e65553e106ddde265f37c69028.jpg)
activity_main.xml内容:
MainActivity.java
其中自定义对话框布局文件login.xml代码:
运行结果
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/430862bf1ef4c05c1105ef998a8ae665.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/29b0eaeccb96f22895e200b748cf111e.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/79b694dfea6f2aeff81c325d218d49ef.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/0657bf8c88ed36d3db7acb92b50890db.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/0b1b4ecb17a9e75753f990853790ecd7.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/546c9323f1d6cc0518fd5248d75d8b2f.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/06c66faa894a6ffab69c39f1f96a1d98.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/2c65a24c3838b85bacdb6f1d2a07e9e9.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/feb1698c56cc79e6071a8de79dbd75ad.jpg)
http://www.oschina.net/question/54100_32486?sort=default&p=1#answers
工程结构图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/bee5d8e65553e106ddde265f37c69028.jpg)
activity_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"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="普通对话框" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="3个按钮对话框" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="输入框" /> <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="单选框" /> <Button android:id="@+id/button5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多选框" /> <Button android:id="@+id/button6" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义对话框" /> <Button android:id="@+id/button7" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="时间对话框" /> <Button android:id="@+id/button8" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="进度条" /> </LinearLayout>
MainActivity.java
package cn.lebo.testdialog; import android.support.v7.app.ActionBarActivity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.*; import android.view.View.*; import android.widget.*; public class MainActivity extends ActionBarActivity implements OnClickListener { private Button btn1 = null; private Button btn2 = null; private Button btn3 = null; private Button btn4 = null; private Button btn5 = null; private Button btn6 = null; private Button btn7 = null; private Button btn8 = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) findViewById(R.id.button1); btn2 = (Button) findViewById(R.id.button2); btn3 = (Button) findViewById(R.id.button3); btn4 = (Button) findViewById(R.id.button4); btn5 = (Button) findViewById(R.id.button5); btn6 = (Button) findViewById(R.id.button6); btn7 = (Button) findViewById(R.id.button7); btn8 = (Button) findViewById(R.id.button8); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); btn6.setOnClickListener(this); btn7.setOnClickListener(this); btn8.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void onClick(View v) { switch(v.getId()){ case R.id.button1: dialog1(); break; case R.id.button2: dialog2(); break; case R.id.button3: dialog3(); break; case R.id.button4: dialog4(); break; case R.id.button5: dialog5(); break; case R.id.button6: dialog6(); break; case R.id.button7: dialog7(); break; case R.id.button8: dialog8(); break; default: break; } } protected void dialog1(){ AlertDialog.Builder builder = new Builder(MainActivity.this); builder.setMessage("确认退出吗"); builder.setTitle("提示"); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); MainActivity.this.finish(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } protected void dialog2(){ AlertDialog.Builder builder2 = new Builder(MainActivity.this); builder2.setTitle("明星调查"); builder2.setMessage("您喜欢范冰冰吗?"); builder2.setIcon(R.drawable.cat); builder2.setPositiveButton("很喜欢", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "我很喜欢范冰冰哦", Toast.LENGTH_LONG).show(); } }); builder2.setNegativeButton("不喜欢", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "我不太喜欢她", Toast.LENGTH_LONG).show(); } }); builder2.setNeutralButton("一般", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "对他不是很感冒", Toast.LENGTH_LONG).show(); } }); builder2.create().show(); } protected void dialog3(){ AlertDialog.Builder builder3 = new Builder(MainActivity.this); builder3.setTitle("请输入信息"); builder3.setIcon(R.drawable.warn); builder3.setView(new EditText(this)); builder3.setPositiveButton("确定", null); builder3.setNegativeButton("取消", null); builder3.create().show(); } protected void dialog4(){ AlertDialog.Builder builder4 = new Builder(MainActivity.this); builder4.setTitle("单选框"); builder4.setIcon(R.drawable.warn); builder4.setSingleChoiceItems(new String[]{"宋茜","古力娜扎"}, 0, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); }}); builder4.setNegativeButton("取消", null); builder4.create().show(); } protected void dialog5(){ AlertDialog.Builder builder5 = new Builder(MainActivity.this); builder5.setTitle("您喜欢哪些明星?"); builder5.setMultiChoiceItems(new String[] {"范冰冰","高圆圆","Angelababy","章子怡","李冰冰","周迅"}, null, null); builder5.setPositiveButton("确定", null); builder5.setNegativeButton("取消", null); builder5.create().show(); } protected void dialog6(){ LayoutInflater layoutinflater = getLayoutInflater(); View view = layoutinflater.inflate(R.layout.login, null); EditText edit_user = (EditText) view.findViewById(R.id.edit_user); EditText edit_passwd = (EditText) view.findViewById(R.id.edit_passwd); final String user_name = edit_user.getText().toString(); final String pass_wd = edit_passwd.getText().toString(); Builder dialog=new AlertDialog.Builder(MainActivity.this); dialog.setTitle("用户登陆"); dialog.setMessage("登陆"); dialog.setView(view); dialog.setPositiveButton("登陆", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { if(user_name.equals("water")&& pass_wd.equals("123abc")){ Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_LONG).show(); } } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog.create(); dialog.show(); } protected void dialog7(){ Dialog dialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener(){ @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(MainActivity.this, "您选择的时间是: " + year +"年"+ monthOfYear + "月" + dayOfMonth + "日" , Toast.LENGTH_LONG).show(); } }, 2015, 8, 1); dialog.show(); } protected void dialog8(){ final ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "正在搜索网络", "请稍候"); new Thread(){ public void run(){ try{ Thread.sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); } finally{ dialog.dismiss(); } } }.start(); } }
其中自定义对话框布局文件login.xml代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="60dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="用户名:" /> <EditText android:id="@+id/edit_user" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="88" android:text="water" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="60dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="密码:" /> <EditText android:id="@+id/edit_passwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="88" android:password="true" android:text="123abc" /> </LinearLayout> </LinearLayout>
运行结果
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/430862bf1ef4c05c1105ef998a8ae665.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/29b0eaeccb96f22895e200b748cf111e.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/79b694dfea6f2aeff81c325d218d49ef.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/0657bf8c88ed36d3db7acb92b50890db.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/0b1b4ecb17a9e75753f990853790ecd7.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/546c9323f1d6cc0518fd5248d75d8b2f.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/06c66faa894a6ffab69c39f1f96a1d98.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/2c65a24c3838b85bacdb6f1d2a07e9e9.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/01/feb1698c56cc79e6071a8de79dbd75ad.jpg)
相关文章推荐
- 关于AndroidStudio导入项目报错的问题
- android Fragment
- Android中fragment切换标题操作
- Android之Handler源码深入分析
- IT蓝豹,android技术特效分享
- Android Studio一些简单设置
- Android自定义View-自动加载更多的ListView
- Android日期选择器
- android手机屏幕横竖屏切换禁止调用生命周期函数方法
- Android Drawable 那些不为人知的高效用法
- Android BitmapShader 实战 实现圆形、圆角图片
- Android ListView的上拉刷新,下拉加载功能(pull - to -refesh)
- Android Xfermode 实战 实现圆形、圆角图片
- Android中加入百度地图SDK
- android开发 缩放到指定比例的尺寸
- android 自定义ViewGroup实现可记载并呈现选择的ListView
- android 自定义ViewGroup实现可记载并呈现选择的ListView
- android自定义布局中的平滑移动
- Android solve设备(手机)旋转过程中出现程序中断问题
- android系统下chmod -R命令无效