对话框的练习(android)

android 中的 AlertDialog 对话框

由 AlertDialog.builder 进行创建,创建后使用 show 显示,使用简单,代码如下

 

对话框的 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"   >

      < TextView
        
android:id ="@+id/username"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"
        android:layout_marginLeft
="20dip"
        android:layout_marginRight
="20dip"
        
        android:text
="账号"
        android:gravity
="left"  
        android:textAppearance
="?android:attr/textAppearanceMedium"
         
/>

      < EditText 
         
android:id ="@+id/username"
         android:layout_height
="wrap_content"
         android:layout_width
="fill_parent"
         android:layout_marginLeft 
= "20dip"
         android:layout_marginRight
="20dip"
         android:scrollHorizontally
="true"
         android:autoText
="false"
         android:capitalize
="none"
         android:gravity
="fill_horizontal"
         android:textAppearance
="?android:attr/textAppearanceMedium"
                  
/> 
                  
      < TextView 
         
android:id ="@+id/password"
         android:layout_width
="wrap_content"
         android:layout_height
="wrap_content"
         android:layout_marginLeft
="20dip"
         android:layout_marginRight
="20dip"
         android:text
="密码"
         android:gravity
="left"
         android:textAppearance
="?android:attr/textAppearanceMedium"
         
         
/>
     
      < EditText 
         
android:id ="@+id/password"
         android:layout_width
="fill_parent"
         android:layout_height
="wrap_content"
         android:layout_marginLeft
="20dip"
         android:layout_marginRight
="20dip"
         android:scrollHorizontally
="true"
         android:autoText
="false"
         android:capitalize
="none"
         android:gravity
="fill_horizontal"
         android:password
="true"          
         
/>

</ LinearLayout >

java 代码

package zziss.android.dialogtest;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

public  class DialogTestActivity  extends Activity {
     /**  Called when the activity is first created.  */
    ProgressDialog m_dialog;
    @Override
     public  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
         // Builder builder = new AlertDialog.Builder(DialogTestActivity.this);
        
// Dialog dialog = builder.create();
        
//  dialog.setTitle("登录提示");
         /* Dialog dialog = new AlertDialog.Builder(DialogTestActivity.this)
        .setTitle("登录提示")
        .setMessage("这里需要登录")
        .setPositiveButton("确定", 
                new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        LayoutInflater factory = LayoutInflater.from(DialogTestActivity.this);
                        final View DialogView = factory.inflate(R.layout.dialog, null);
                        
                        AlertDialog dlg = new AlertDialog.Builder(DialogTestActivity.this)
                        .setTitle("登录框")
                        .setView(DialogView)
                        .setPositiveButton("确定", 
                                new DialogInterface.OnClickListener() {
                                    
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // TODO Auto-generated method stub
                                        m_dialog = ProgressDialog.show(
                                                DialogTestActivity.this, 
                                                "请等待...", "正在为您登录...",true);
                                        new Thread()
                                        {
                                            public void run()
                                            {
                                                try
                                                {
                                                    sleep(3000);
                                                }
                                                catch(Exception e)
                                                {
                                                    e.printStackTrace();
                                                }
                                                finally
                                                {
                                                    m_dialog.dismiss();
                                                }
                                            }
                                        }.start(); // end thread
                                    } // end ok click
                                }
                                )
                        .setNegativeButton("取消",
                                new DialogInterface.OnClickListener() {
                                    
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // TODO Auto-generated method stub
                                        DialogTestActivity.this.finish();
                                    }
                                }
                                ).create();
                        dlg.show();
                        
                    };
                }
                
                )
                .setNeutralButton("退出", 
                        new DialogInterface.OnClickListener() {
                            
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                DialogTestActivity.this.finish();
                            }
                        }
                        ).create();
        dialog.show();
*/
        initDialogClick();
        AlertDialog.Builder builder =  new AlertDialog.Builder(DialogTestActivity. this);
        builder.setTitle("登录对话框");
        builder.setMessage("这里需要登录")
        .setPositiveButton("确定",dialogclick )
        .setNegativeButton("取消", dialogclick);
        Dialog dialog = builder.create();
        dialog.show();
        
        
    }
    
     private DialogInterface.OnClickListener dialogclick;
     private DialogInterface.OnClickListener dialogLoginClick;
     private  void initDialogClick()
    {
        dialogclick =  new DialogInterface.OnClickListener() {
            
            @Override
             public  void onClick(DialogInterface dialog,  int which) {
                 //  TODO Auto-generated method stub
                 if (which == dialog.BUTTON_POSITIVE)
                {
                    showLoginDialog();
                }
                 if (which == dialog.BUTTON_NEGATIVE)
                {
                    showMessage("点击了取消按纽");
                }
            }
        };
        
        dialogLoginClick = new DialogInterface.OnClickListener() {
            
            @Override
             public  void onClick(DialogInterface dialog,  int which) {
                 //  TODO Auto-generated method stub
                 if (which == dialog.BUTTON_POSITIVE)
                {
                    showProgressDialog();
                }
                 if (which == dialog.BUTTON_NEGATIVE)
                {
                    dialog.dismiss();
                }
            }
        }; 
        
    }  //  end initDialogClick
    
     private  void showLoginDialog()
    {
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View logindialog = inflater.inflate(R.layout.dialog,  null);
        AlertDialog login =  new AlertDialog.Builder( this)
        .setTitle("请登录")
        .setView(logindialog)
        .setPositiveButton("登录", dialogLoginClick)
        .setNegativeButton("取消", dialogLoginClick)
        .create();
        login.show();
    }
    
    
     private  void showProgressDialog()
    {
        m_dialog = ProgressDialog.show(
                DialogTestActivity. this
                "请等待...", "正在为您登录...", true);
         new Thread()
        {
             public  void run()
            {
                 try
                {
                    sleep(3000);
                }
                 catch(Exception e)
                {
                    e.printStackTrace();
                }
                 finally
                {
                    m_dialog.dismiss();
                }
            }
        }.start();  //  end thread
    }
    
     private  void showMessage(String str)
    {
        Toast toast = Toast.makeText( this, str, Toast.LENGTH_SHORT);
        toast.show();
    }
}

上面的注释中的代码是书上的,不太好看懂,所以就拆开了

 

 

 

点击alertdialog之外其他屏幕,alertdialog消失/dismiss

很简单,设置一下alertDialog的属性就行:

AlertDialog alert = builder.create();
alert.setCanceledOnTouchOutside(true);//详细信息可以查看官方文档
alert.show();

 

 

转载于:https://www.cnblogs.com/zziss/archive/2011/12/27/2304007.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值