Android對話方塊總結

來源:互聯網
上載者:User

標籤:dialog   對話方塊   自訂   android   

一、什麼是對話方塊?
一種次要視窗,包含按鈕和各種選項,通過它們可以完成特定命令或任務。 尋找和替換對話方塊 對話方塊與視窗有區別,它沒有最大化按鈕、沒有最小化按鈕、大都不能改變形狀大小。(“開啟檔案”對話方塊是可以改變大小的) 對話方塊:是人機交流的一種方式,使用者對對話方塊進行設定,電腦就會執行相應的命令。對話方塊中有單選框、複選框等。
要我說,對話方塊,就是一個用於和使用者進行對話的小框框。大家見到最多的那種是彈出對話方塊。
二、對話方塊有什麼用?
對話方塊不會阻塞主線程,可以和使用者進行簡單互動,且給人一種清新的感覺,好的對話方塊不僅不會影響使用者體驗,反而會讓使用者喜歡上這種被打擾的感覺。

三、怎麼用?

         

1、系統對話方塊
AlertDialog
ProgressDialog
DatePickerDialog
TimePickerDialog
2、自訂對話方塊
a、使用LayoutInflate和Builder的setView方法實現
b、繼承Dialog類複寫setContentView實現
執行個體:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/show1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/button_background"
        android:text="dialog1" />

    <Button
        android:id="@+id/show2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/button_background"
        android:text="dialog2" />

    <Button
        android:id="@+id/show3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/button_background"
        android:text="dialog3" />

</LinearLayout>

activity_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="標題"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="這是一個自訂布局的AlertDialog" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="有木有很好玩" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="想怎麼布局就怎麼布局"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_background"
        android:text="確定" />

</LinearLayout>

MainActivity.java
public class MainActivity extends Activity {
    private Button show1, show2, show3;
    DatePickerDialog m = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show1 = (Button) findViewById(R.id.show1);
        show2 = (Button) findViewById(R.id.show2);
        show3 = (Button) findViewById(R.id.show3);
        show1.setOnClickListener(mClickListener);
        show2.setOnClickListener(mClickListener);
        show3.setOnClickListener(mClickListener);
    }

    private View.OnClickListener mClickListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            int id = arg0.getId();
            switch (id) {
            case R.id.show1:
                showDialog1();
                break;
            case R.id.show2:
                showDialog2();
                break;
            case R.id.show3:
                showDialog3();
                break;
            default:
                break;
            }
        }
    };

    private void showDialog1() {
        new AlertDialog.Builder(this).setTitle("dialog 1")
                .setMessage("這是最簡單的AlertDialog").setPositiveButton("確定", null)
                .setNeutralButton("什麼", null).setNegativeButton("取消", null)
                .create().show();
    }
    
    private void showDialog2(){
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.activity_dialog, null);
        final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create();
        Button ok = (Button) view.findViewById(R.id.button1);
        ok.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    
    private void showDialog3(){
        new MyDialog(this).show();
    }
    
    private class MyDialog extends Dialog{

        public MyDialog(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            this.setContentView(R.layout.activity_dialog);
            this.setTitle("標題");
            Button b = (Button) findViewById(R.id.button1);
            b.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    dismiss();
                }
            });
        }
    }
}

四、注意總結
注意:
1、不要使對話方塊泛濫
2、自訂對話方塊時要注意dismiss()或則不用時remove()
3、還有一種定義對話方塊的方法,那就是把Activity的主題設定為Theme.Dialog
總結:
在這個看顏的時代,要想成為一個好的程式員,首先要成為一名好的設計師。

Android對話方塊總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.