[Android控制項]Dialogue大全

來源:互聯網
上載者:User

標籤:android   c   class   code   a   http   

 

在Android開發中,我們經常會需要在Android介面上彈出一些對話方塊,比如詢問使用者或者讓使用者選擇。這些功能我們叫它Android Dialog對話方塊,在我們使用Android的過程中,我歸納了一下,Android Dialog的類型無非也就7種,下面我分別向大家介紹這7種Android Dialog對話方塊的使用方法,希望對大家能有所協助。

1.該效果是當按返回按鈕時彈出一個提示,來確保無誤操作,採用常見的對話方塊樣式。

建立dialog對話方塊方法代碼如下:

protected void dialog() {    AlertDialog.Builder builder = new Builder(Main.this);    builder.setMessage("確認退出嗎?");    builder.setTitle("提示");    builder.setPositiveButton("確認", new OnClickListener() {    @Override    public void onClick(DialogInterface dialog, int which) {    dialog.dismiss();    Main.this.finish();    }    });    builder.setNegativeButton("取消", new OnClickListener() {    @Override    public void onClick(DialogInterface dialog, int which) {    dialog.dismiss();    }    });    builder.create().show();    }

在onKeyDown(int keyCode, KeyEvent event)方法中調用此方法

public boolean onKeyDown(int keyCode, KeyEvent event) {    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {     dialog();    }    return false;   }

 

2.改變了對話方塊的圖表,添加了三個按鈕
建立dialog的方法代碼如下:

Dialog dialog = new AlertDialog.Builder(this).setIcon(    android.R.drawable.btn_star).setTitle("喜好調查").setMessage(    "你喜歡李連杰的電影嗎?").setPositiveButton("很喜歡",    new OnClickListener() {    @Override    public void onClick(DialogInterface dialog, int which) {    // TODO Auto-generated method stub    Toast.makeText(Main.this, "我很喜歡他的電影。",    Toast.LENGTH_LONG).show();    }    }).setNegativeButton("不喜歡", new OnClickListener() {    @Override    public void onClick(DialogInterface dialog, int which) {    // TODO Auto-generated method stub    Toast.makeText(Main.this, "我不喜歡他的電影。", Toast.LENGTH_LONG)    .show();    }    }).setNeutralButton("一般", new OnClickListener() {    @Override    public void onClick(DialogInterface dialog, int which) {    // TODO Auto-generated method stub    Toast.makeText(Main.this, "談不上喜歡不喜歡。", Toast.LENGTH_LONG)    .show();    }    }).create();    dialog.show();

 

3.資訊內容是一個簡單的View類型
建立dialog方法的代碼如下:

new AlertDialog.Builder(this).setTitle("請輸入").setIcon(    android.R.drawable.ic_dialog_info).setView(    new EditText(this)).setPositiveButton("確定", null)    .setNegativeButton("取消", null).show();

 

4.資訊內容是一組單選框
建立dialog方法的代碼如下:

new AlertDialog.Builder(this).setTitle("複選框").setMultiChoiceItems(    new String[] { "Item1", "Item2" }, null, null)    .setPositiveButton("確定", null)    .setNegativeButton("取消", null).show();

 

5.資訊內容是一組多選框
建立dialog方法的代碼如下:

new AlertDialog.Builder(this).setTitle("單選框").setIcon(    android.R.drawable.ic_dialog_info).setSingleChoiceItems(    new String[] { "Item1", "Item2" }, 0,    new DialogInterface.OnClickListener() {    public void onClick(DialogInterface dialog, int which) {    dialog.dismiss();    }    }).setNegativeButton("取消", null).show();

 

6.資訊內容是一組簡單清單項目
建立dialog的方法代碼如下:

new AlertDialog.Builder(this).setTitle("列表框").setItems(    new String[] { "Item1", "Item2" }, null).setNegativeButton(    "確定", null).show();

 

7.資訊內容是一個自訂的布局
dialog布局檔案代碼如下:

<?xml version="1.0" encoding="utf-8"?>   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_height="wrap_content" android:layout_width="wrap_content"    android:background="#ffffffff" android:orientation="horizontal"    android:id="@+id/dialog">    <TextView android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:id="@+id/tvname" android:text="姓名:" />    <EditText android:layout_height="wrap_content"    android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/>   </LinearLayout>

 

建立dialog方法的代碼如下:

LayoutInflater inflater = getLayoutInflater();    View layout = inflater.inflate(R.layout.dialog,    (ViewGroup) findViewById(R.id.dialog));    new AlertDialog.Builder(this).setTitle("自訂布局").setView(layout)    .setPositiveButton("確定", null)    .setNegativeButton("取消", null).show();

 

好了,以上7種Android dialog對話方塊的使用方法就介紹到這裡了,基本都全了,如果大家在android開發過程中遇到dialog的時候就可以拿出來看看。

聯繫我們

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