【原創】Android 對話方塊的使用

來源:互聯網
上載者:User

標籤:

對話方塊即Dialog 、google的官方解釋:A dialog is usually a small window that appears in front of the current Activity. The underlying Activity loses focus and the dialog accepts all user interaction.

翻譯過來就是:對話方塊是當前Activity之上的小視窗,此時對話方塊接受使用者的操作而Activity得不到使用者的反饋。

一般而言我們不會直接執行個體化Dialog 我們用的是Dialog的子類 下面是一些Dialog的常用子類

對於對話方塊:大致分三步:建立對話方塊、顯示對話方塊show()、dismiss()對話方塊

AlertDialog:

A dialog that can manage zero, one, two, or three buttons, and/or a list of selectable items that can include checkboxes or radio buttons. The AlertDialog is capable of constructing most dialog user interfaces and is the suggested dialog type

progressDialog:

A dialog that displays a progress wheel or progress bar. Because it‘s an extension of the AlertDialog, it also supports buttons.

DatePickerDialog:

A dialog that allows the user to select a date. 

TimePickerDiaog:

A dialog that allows the user to select a time. 

當然我們也會使用自己自訂的對話方塊。

各種對話方塊使用如下:

代碼如下 點擊相應按鈕建立對話方塊

package leemo.DialogEp;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.ProgressDialog;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;/* * 練習操作Dialog的使用 */public class DialogEpActivity extends Activity {/** Called when the activity is first created. */protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// setContentView(R.layout.main);Button btalert = (Button) findViewById(R.id.btalert);btalert.setText("點擊建立Alert Dialog");btalert.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubcreateAlertDialog();}});Button btlist = (Button) findViewById(R.id.btlist);btlist.setText("點擊建立ListDialog");btlist.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubcreateListDialog();}});Button btcb = (Button) findViewById(R.id.btcb);btcb.setText("點擊建立帶checkbox的對話方塊");btcb.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubcreateSingleCheckBoxDialog();}});Button btpb = (Button) findViewById(R.id.btpb);btpb.setText("點擊建立進度條");btpb.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubcreatepbDialog();}});Button btcusdialog = (Button) findViewById(R.id.btcusdialog);btcusdialog.setText("點擊建立自訂的對話方塊");btcusdialog.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubcreatecusDialog();}});}public void createAlertDialog() {AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setMessage("Are you sure want to exit").setCancelable(false).setPositiveButton("Yes",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {// TODO Auto-generated method stubDialogEpActivity.this.finish();}}).setNegativeButton("No", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubdialog.cancel();}});builder.create().show();}public void createListDialog() {final CharSequence[] items = { "Red", "Blue", "Green" };AlertDialog.Builder builder = new AlertDialog.Builder(DialogEpActivity.this);builder.setTitle("Select A Color");builder.setItems(items, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(getApplication(), items[which],Toast.LENGTH_LONG).show();}});builder.create().show();}public void createSingleCheckBoxDialog() {final CharSequence[] items = { "Red", "Bule", "Green" };AlertDialog.Builder bd = new AlertDialog.Builder(DialogEpActivity.this);bd.setTitle("Select A Color");bd.setSingleChoiceItems(items, -1,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubdialog.dismiss();Toast.makeText(getApplicationContext(), items[which],Toast.LENGTH_SHORT).show();}});bd.create().show();}public void createpbDialog() {ProgressDialog dialog = ProgressDialog.show(DialogEpActivity.this,"loading", "loading please waiting");dialog.show();}public void createcusDialog() {Context mContext = DialogEpActivity.this;Dialog dialog = new Dialog(mContext);dialog.setContentView(R.layout.dialog);dialog.setTitle("Custom Dialog");TextView text = (TextView) dialog.findViewById(R.id.text);text.setText("Hello, this is a custom dialog!");ImageView image = (ImageView) dialog.findViewById(R.id.image);image.setImageResource(R.drawable.ic_launcher);Button btcs = (Button) dialog.findViewById(R.id.btcs);btcs.setText("點擊建立Toast");btcs.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubToast.makeText(DialogEpActivity.this, "is okay",Toast.LENGTH_SHORT).show();}});dialog.show();}}

 建立自訂對話方塊時,要自己定義布局檔案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:id="@+id/layout_root"              android:orientation="horizontal"              android:layout_width="fill_parent"              android:layout_height="fill_parent"              android:padding="10dp"              >    <ImageView android:id="@+id/image"               android:layout_width="wrap_content"               android:layout_height="fill_parent"               android:layout_marginRight="10dp"               />    <TextView android:id="@+id/text"              android:layout_width="wrap_content"              android:layout_height="fill_parent"              android:textColor="#FFF"              />    <Button         android:id="@+id/btcs"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        /></LinearLayout>

 效果如

 

 

 

 

 

 

【原創】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.