對話方塊的練習(android)

來源:互聯網
上載者:User

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();

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.