今天学习了Dialog组件,并且将Dialog做了一下总结,下面是总结的内容和Demo
AlertDialog:功能最丰富、实际应用最广的对话框
ProgressDialog:进度对话框,
DatePickerDialog:日期选择对话框
TimePickerDialog:时间选择对话框
AlertDialog提供了四种预定义对话框
》带消息、带N个按钮的提示对话框
》带列表、带N个按钮的列表对话框
》带多个单选列表项,带N个按钮的对话框
》带多个多选列表项,带N个按钮的对话框
除此之外,可创建界面自定义的对话框
创建对话框的过程:
1、定义一个Alertdialog.Builder对象
2、
//设置图标
setIcon(),
//设置标题
setTitle(),
//设置显示内容
setMessage()
//设置确定按钮
setPositiveButton
//设置取消按钮
setNegativeButton
//创建装饰性按钮
setNeutralButton(CharSequence text,DialogInterface.OnClickListener listener);
//创建显示对话框
create().show()
实现一个多种对话框集锦。包括(列表对话框,单选列表对话框,多选对话框,自定义对话框)点击相应的按钮出来相应的对话框。
步骤
1、定义xml文件,完成button布局。
2、重写Activity中的onCreateDialog方法,返回Dialog对象。调用showDialog方法便可以显示对话框。
MainActivity.java
package lzl.edu.com.dialogdemos; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TableLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener{ public static final int LISTDIALOG = 1; public static final int SINGLEDIALOG = 2; public static final int MULTIDIALOG = 3; public static final int LOGINDIALOG = 4; TextView mtextView; AlertDialog.Builder builder ; Button listDialog,singleDialog,multiDialog,loginDialog; EditText mEditText,userName,password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } void init(){ listDialog = (Button)findViewById(R.id.listdialog); singleDialog=(Button)findViewById(R.id.singledialog); multiDialog=(Button)findViewById(R.id.multidialog); loginDialog=(Button)findViewById(R.id.logindialog); mtextView = (TextView)findViewById(R.id.mtextview); mEditText = (EditText)findViewById(R.id.medit); listDialog.setOnClickListener(this); singleDialog.setOnClickListener(this); multiDialog.setOnClickListener(this); loginDialog.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.listdialog: showDialog(LISTDIALOG); break; case R.id.singledialog: showDialog(SINGLEDIALOG); break; case R.id.multidialog: showDialog(MULTIDIALOG); break; case R.id.logindialog: showDialog(LOGINDIALOG); break; default:break; } } @Override protected Dialog onCreateDialog(int id) { switch (id){ case LISTDIALOG: return OnListDialog(); case SINGLEDIALOG: return OnSingleDialog(); case MULTIDIALOG: return OnMutiDialog(); case LOGINDIALOG: return OnLoginDialog(); default:break; } return null; } Dialog OnListDialog(){ builder = new AlertDialog.Builder(this); builder.setTitle("英语六级查询"); final String[] contents =new String[]{"99宿舍","中国教育厅","教育部考试中心"}; builder.setItems(contents, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: mtextView.setBackgroundColor(Color.RED); mtextView.setText(contents[which]); break; case 1: mtextView.setBackgroundColor(Color.GREEN); mtextView.setText(contents[which]); break; case 2: mtextView.setBackgroundColor(Color.YELLOW); mtextView.setText(contents[which]); break; default: break; } } }); builder.setPositiveButton("确定",null); return builder.create(); } Dialog OnSingleDialog(){ builder = new AlertDialog.Builder(this); final String[] contents =new String[]{"99宿舍","中国教育厅","教育部考试中心"}; builder.setTitle("英语六级查询"); builder.setSingleChoiceItems(contents, 1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: mtextView.setBackgroundColor(Color.RED); mtextView.setText(contents[which]); break; case 1: mtextView.setBackgroundColor(Color.GREEN); mtextView.setText(contents[which]); break; case 2: mtextView.setBackgroundColor(Color.YELLOW); mtextView.setText(contents[which]); break; default: break; } } }); builder.setPositiveButton("确定",null); return builder.create(); } Dialog OnMutiDialog(){ builder = new AlertDialog.Builder(this); builder.setTitle("七夕节礼物"); final String[] qixiGift = new String[]{"情侣电影票","情侣套餐","情侣手环","情侣戒指"}; final boolean[] checkStates = new boolean[]{false,false,false,false}; builder.setMultiChoiceItems(qixiGift, checkStates, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { String result = "您选择的七夕礼物是:"; for (int i = 0; i < checkStates.length; i++) { if (checkStates[i]) { result += qixiGift[i]; } } mEditText.setText(result); } }); return builder.create(); } Dialog OnLoginDialog(){ builder = new AlertDialog.Builder(this); //将登录的布局文件加载到界面中 LayoutInflater inflator = LayoutInflater.from(this); TableLayout loginForm = (TableLayout)inflator.inflate(R.layout.logindialog,null); userName =(EditText)loginForm.findViewById(R.id.username); password =(EditText)loginForm.findViewById(R.id.password); builder.setView(loginForm); builder.setPositiveButton("登录", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String name = userName.getText().toString(); Toast.makeText(MainActivity.this, name +"准备登录", Toast.LENGTH_LONG).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"取消登录",Toast.LENGTH_LONG).show(); } }); return builder.create(); } }logindialog.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout android:id="@+id/login_layout" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="用户登录" /> <TableRow android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:text="用户名" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/username" android:hint="请输入用户名" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </TableRow> <TableRow android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:text="密码" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:hint="请输入密码" android:id="@+id/password" android:inputType="textPassword" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </TableRow> </TableLayout>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"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/listdialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="列表对话框" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> <Button android:id="@+id/singledialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单选对话框" android:layout_below="@+id/logindialog" android:layout_alignParentStart="true" /> <Button android:id="@+id/multidialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="多选对话框" android:layout_below="@+id/singledialog" android:layout_toStartOf="@+id/singledialog" android:layout_marginTop="54dp" /> <Button android:id="@+id/logindialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录对话框" android:layout_below="@+id/listdialog" android:layout_alignParentStart="true" /> <TextView android:id="@+id/mtextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp"/> <EditText android:id="@+id/medit" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textSize="30sp"/> </LinearLayout> </RelativeLayout>效果图:
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u011521890/article/details/47805109