Android入门开发之Dialog实例

来源:互联网 发布:泛雅网络教学平台app 编辑:程序博客网 时间:2024/06/06 01:55

转载请注明出处:http://write.blog.csdn.net/postedit/52476237

一、Dilog使用实例

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"     tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="10pt"        android:text="对话框测试" />    <Button         android:id="@+id/simpleButton"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:text="简单对话框"/>    <Button         android:id="@+id/listButton"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:text="列表对话框"/>    <Button         android:id="@+id/multiButton"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:text="多选对话框"/>    <Button         android:id="@+id/loginButton"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:text="登录对话框"/>    <Button         android:id="@+id/timerButton"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:text="时间对话框"/>    <Button         android:id="@+id/popupButton"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:text="弹出对话框"/>    <Button         android:id="@+id/customButton"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:text="自定义对话框"/></LinearLayout>
user_login.xml文件:为登录对话框提供布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/editText1"        android:layout_marginLeft="28dp"        android:layout_marginTop="56dp"        android:text="密码:" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView2"        android:layout_alignParentTop="true"        android:layout_marginTop="100dp"        android:text="账号:" />    <EditText        android:id="@+id/editText2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/textView2"        android:layout_alignBottom="@+id/textView2"        android:layout_toRightOf="@+id/textView2"        android:ems="10" />    <EditText        android:id="@+id/editText1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/textView1"        android:layout_alignBottom="@+id/textView1"        android:layout_alignLeft="@+id/editText2"        android:ems="10" >    </EditText>    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView2"        android:layout_below="@+id/editText2"        android:layout_marginLeft="32dp"        android:layout_marginTop="48dp"        android:text="登录" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/button1"        android:layout_alignBottom="@+id/button1"        android:layout_alignRight="@+id/editText2"        android:layout_marginRight="16dp"        android:text="注册" /></RelativeLayout>
popup.xml文件:为弹出窗口提供布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content" >    <TextView        android:id="@+id/Text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:text="我是弹出窗口"        android:textSize="10pt" />    <Button        android:id="@+id/closeButton"        android:layout_width="100dp"        android:layout_height="60dp"        android:layout_alignBaseline="@+id/sureButton"        android:layout_alignBottom="@+id/sureButton"        android:layout_alignParentRight="true"        android:layout_marginRight="60dp"        android:text="关闭" />    <Button        android:id="@+id/sureButton"        android:layout_width="100dp"        android:layout_height="60dp"        android:layout_alignParentLeft="true"        android:layout_below="@+id/Text"        android:layout_marginLeft="38dp"        android:layout_marginTop="30dp"        android:text="确定" /></RelativeLayout>

MainActivity.java源文件:

package com.example.alertdialogtest;/** * copyright : qiao * date: 2016/9/4 */import java.lang.reflect.Field;import java.util.Calendar;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.app.DatePickerDialog;import android.app.Dialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.DatePicker;import android.widget.PopupWindow;import android.widget.RelativeLayout;import android.widget.Toast;public class MainActivity  extends Activity {protected static final int PROGRESS_DIALOG = 0x112;private Button simpleButton;private Button listButton;private Button multiButton;private Button loginButton;private Button timerButton;private Button progressButton;private Button customButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initSimpleButton();initListButton();initMultiButton();initLoginButton();initTimerButton();initPopupButton();initCustomButton();}//初始化简单对话框    private void initSimpleButton(){    simpleButton = (Button)findViewById(R.id.simpleButton);     final Builder builder =new  AlertDialog.Builder(this);    simpleButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {                   builder.setIcon(R.drawable.ic_launcher);                   builder.setTitle("普通对话框");                   builder.setMessage("Hello");                   builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {   @Override   public void onClick(DialogInterface dialog, int which) {       try                          {                             Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing" );                           field.setAccessible( true );                             field.set(dialog, true );                             dialog.dismiss();                         }                         catch (Exception e)                         {                             e.printStackTrace();                         }       dialog.cancel();   }   }) ;                   builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {@Override     public void onClick(DialogInterface dialog, int which) {    try                       {                          Field field = dialog.getClass().getSuperclass().getDeclaredField(  "mShowing" );                          field.setAccessible( true );                           field.set(dialog, false );                          dialog.dismiss();                      }                      catch (Exception e)                      {                          e.printStackTrace();                      }      Toast.makeText(getBaseContext() , "你点击了确定", Toast.LENGTH_LONG).show();     }    });                       builder.show(); }});    }  //初始化列表对话框    private void initListButton(){    listButton = (Button)findViewById(R.id.listButton);    final Builder builder = new AlertDialog.Builder(this);    final String[] names = new String[]{"刘德华","古天乐","吴彦祖"};    listButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {builder.setIcon(R.drawable.ic_launcher);builder.setTitle("列表选项对话框");builder.setItems(names,        new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {                            Toast.makeText(MainActivity.this, "你点击了:"+names[which], Toast.LENGTH_LONG).show();}});builder.show();}});    }  //初始化多选按钮对话框    private void initMultiButton(){     multiButton = (Button)findViewById(R.id.multiButton);     final Builder builder = new AlertDialog.Builder(this);     final String[] names = new String[]{"刘德华","古天乐","吴彦祖"};     final boolean[] checkStatus = new boolean[]{false,false,false};     multiButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {   builder.setIcon(R.drawable.ic_launcher);   builder.setTitle("多选对话框");   builder.setMultiChoiceItems(names, checkStatus, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {     String checked ="";                             for(int i = 0 ; i < checkStatus.length ; i++){                             if(checkStatus[i]){                             checked = checked+names[i]+"/";                             }                             }                             Toast.makeText(MainActivity.this, "你选择了:"+checked, Toast.LENGTH_LONG).show();  }});   builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "你点击了确定按钮:", Toast.LENGTH_LONG).show();}});    builder.show();}});    }  //初始化登录对话框    private void initLoginButton(){    loginButton = (Button)findViewById(R.id.loginButton);    final Builder builder = new AlertDialog.Builder(this);    loginButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {                   builder.setIcon(R.drawable.ic_launcher);                   builder.setTitle("登录对话框");                   RelativeLayout rl = (RelativeLayout)getLayoutInflater().inflate(R.layout.user_login, null);   builder.setView(rl);   builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}});   builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}});   builder.create().show();}});    }  //初始化日期时间对话框    private void initTimerButton(){      timerButton = (Button)findViewById(R.id.timerButton);      timerButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Calendar c =  Calendar.getInstance();  new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {@Overridepublic void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {}}, c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();}});    }  //初始化弹出对话框    private void initPopupButton(){    View root = this.getLayoutInflater().inflate(R.layout.popup, null);    progressButton=(Button)findViewById(R.id.popupButton);    final PopupWindow p =new PopupWindow(root,500,500);    progressButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {                  p.showAsDropDown(v);}});    root.findViewById(R.id.closeButton).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {               p.dismiss();}});    }  //初始化自定义对话框    private void initCustomButton(){    final int ListDialog = 0x113;    customButton = (Button)findViewById(R.id.customButton);    customButton.setOnClickListener(new OnClickListener() {@SuppressWarnings("deprecation")@Overridepublic void onClick(View v) { showDialog(ListDialog);}});    }    @Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}@Override@Deprecatedprotected Dialog onCreateDialog(int id, Bundle args) {           if(id == 0x113){           String[] names = new String[]{"刘德华","古天乐","吴彦祖"};            Builder builder = new AlertDialog.Builder(this);            builder.setIcon(R.drawable.ic_launcher);            builder.setTitle("列表对话框");            builder.setItems(names, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}});            return builder.create();              }  return null;}}

  运行效果:

                                                                


                                               

0 0