Android常用的五種彈出對話方塊

來源:互聯網
上載者:User

Android常用的五種彈出對話方塊

一個Android開發中常用對話方塊的小例子,共有五種對話方塊:普通彈出對話方塊,單選對話方塊,多選對話方塊,輸入對話方塊及進度條樣式對話方塊:

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


android:id="@+id/common_dialog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="普通對話方塊"
android:textSize="16sp"
android:layout_marginTop="10dp" />


android:id="@+id/radio_dialog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="單選對話方塊"
android:textSize="16sp"
android:layout_marginTop="10dp" />


android:id="@+id/check_dialog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="多選對話方塊"
android:textSize="16sp"
android:layout_marginTop="10dp" />


android:id="@+id/input_dialog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="輸入文字對話方塊"
android:textSize="16sp"
android:layout_marginTop="10dp" />


android:id="@+id/progress_dialog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="進度條對話方塊"
android:textSize="16sp"
android:layout_marginTop="10dp" />


 

下面是輸入內容的簡單布局activity_input.xml

xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


android:id="@+id/uname"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


android:id="@+id/upass"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />


 

代碼及注釋:

public class MainActivity extends Activity implements OnClickListener {
/**單選框類比標題 大學*/
private final static int CHECKED_ENU = 0;
/**單選框類比標題 高中*/
private final static int CHECKED_SEL = 1;
/**單選框類比標題 初中*/
private final static int CHECKED_CHU = 2;
/**複選按鈕狀態為全選 */
private boolean[] checked = { true, true, true, false };
/**類比的進度值 */
private int progressNumber;
/**進度對話方塊 */
private ProgressDialog progressDialog;
/**對應按鈕*/
private Button commonBtn, radioBtn, checkBtn, inputBtn, progressBtn;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initListeners();
}


/**初始化UI控制項*/


private void initViews() {
this.commonBtn = (Button) findViewById(R.id.common_dialog);
this.radioBtn = (Button) findViewById(R.id.radio_dialog);
this.checkBtn = (Button) findViewById(R.id.check_dialog);
this.inputBtn = (Button) findViewById(R.id.input_dialog);
this.progressBtn = (Button) findViewById(R.id.progress_dialog);
}


/**註冊按鈕監聽事件*/
private void initListeners() {
this.commonBtn.setOnClickListener(this);
this.radioBtn.setOnClickListener(this);
this.checkBtn.setOnClickListener(this);
this.inputBtn.setOnClickListener(this);
this.progressBtn.setOnClickListener(this);
}


/**普通對話方塊 */
private Dialog buildAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("對話方塊");
builder.setMessage("您的密碼不對!!");


ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.mm1);
/**設定背景圖片*/
builder.setView(imageView);
/**左邊按鈕*/
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是左邊確定按鈕!");
}
});
/**中間按鈕*/
builder.setNeutralButton("詳情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是中間詳情按鈕!");
}
});
/**右邊按鈕*/
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
setTitle("您點擊的是右邊取消按鈕!");
}
});
return builder.create();
}


/**選項按鈕彈出框 */
private Dialog buildAlertDialog_radio() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("對話方塊");
/**選項按鈕,預設高中被選中*/
builder.setSingleChoiceItems(new String[] { "大學", "高中", "初中", "小學" }, 1, new DialogInterface.OnClickListener() {


@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch (which) {
case CHECKED_ENU:
setTitle("大學");
break;
case CHECKED_SEL:
setTitle("高中");
break;
case CHECKED_CHU:
setTitle("初中");
break;
default:
setTitle("小學");
break;
}
}
});


builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是左邊確定按鈕!");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是右邊取消按鈕!");
}
});
return builder.create();
}


/**可以多選按鈕彈出框 */
private Dialog buildAlertDialog_checkbox() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("對話方塊");
/**複選按鈕*/
builder.setMultiChoiceItems(new String[] { "大學", "高中", "初中", "小學" }, checked, new DialogInterface.OnMultiChoiceClickListener() {


@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
setTitle("which=" + which + "-----" + "isChecked=" + isChecked);
}
});


builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊了確定按鈕!");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
setTitle("您點擊的是了取消按鈕!");
}
});
return builder.create();
}


/**含可以輸入文本的彈出框 */
private Dialog buildAlertDialog_input() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("對話方塊");
LayoutInflater inflater = LayoutInflater.from(this);
builder.setView(inflater.inflate(R.layout.activity_input, null));
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是確定按鈕!");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是取消按鈕!");
}
});
return builder.create();
}


/**進度對話方塊 */
private Dialog buildAlertDialog_progress() {
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("進度條");
progressDialog.setMessage("正在下載...........");
/**進度條樣式 */
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
/**模糊效果 */
progressDialog.setIndeterminate(false);
return progressDialog;
}


/**每隔0.3秒更新一次進度 */
public void updateProgress() {
new Thread() {
@Override
public void run() {
try {
while (progressNumber <= 100) {
progressDialog.setProgress(progressNumber++);
Thread.sleep(300);
super.run();
}
/**下載完後,關閉下載框 */
progressDialog.cancel();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.common_dialog:
buildAlertDialog().show();
break;
case R.id.radio_dialog:
buildAlertDialog_radio().show();
break;
case R.id.check_dialog:
buildAlertDialog_checkbox().show();
break;
case R.id.input_dialog:
buildAlertDialog_input().show();
break;
case R.id.progress_dialog:
buildAlertDialog_progress().show();
updateProgress();
break;
default:
break;
}
}
}

聯繫我們

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