剖析Android中 一個簡單對話框
作者:佚名
對于windows來說,對話框是一種次要窗口,包含按鈕和各種選項,通過它們可以完成特定命令或任務(wù)。 查找和替換對話框 對話框與窗口有區(qū)別,它沒有最大化按鈕、沒有最小化按鈕、大都不能改變形狀大小。本文講述的是關(guān)于android。
一個簡單講述對話框的Android實例,剖析其中的原理,下文直接是代碼。
***步:先配置項目中的res文件夾中values/strings.xml
Java代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">普通對話框</string>
- <string name="button">顯示普通對話框</string>
- <string name="title">普通對話框</string>
- <string name="ok">確定</string>
- <string name="message">這是一個普通對話框</string>
- </resources>
第二步:配置項目中的res文件夾中l(wèi)ayout/main.xml 用來創(chuàng)建輸入框和“顯示普通對話框”按鈕
java代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <EditText android:id="@+id/editText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/message"/>
- <Button android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/button"/>
- </LinearLayout>
第三步:編寫src文件夾包底下的.java文件
java代碼:
- package eoe.demo;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class CommonDialogActivity extends Activity {
- protected static final int COMMON_DIALOG = 1;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button button= (Button)findViewById(R.id.button);
- View.OnClickListener listener = new View.OnClickListener() {
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- showDialog(COMMON_DIALOG);
- }
- };
- button.setOnClickListener(listener);
- }
- /*
- * 使用onCreateDialog(int)來管理對話框的狀態(tài),
- * 那么每次對話框被解除時,
- * 該對話框?qū)ο蟮臓顟B(tài)會被Activity保存. 調(diào)用
- * removeDialog(int)將所有該對象的內(nèi)部引用
- * 移除 如本程序那樣,如果不加removeDialog,
- * 那么顯示的是***次的內(nèi)容
- */
- protected Dialog onCreateDialog(int id){
- AlertDialog dialog=null;
- EditText editText = (EditText) findViewById(R.id.editText);
- switch(id){
- case COMMON_DIALOG:
- Builder builder = new AlertDialog.Builder(this);
- builder.setIcon(R.drawable.icon);
- builder.setMessage(editText.getText());
- builder.setTitle(R.string.title);
- DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface arg0, int arg1) {
- // TODO Auto-generated method stub
- removeDialog(COMMON_DIALOG);
- }
- };
- builder.setPositiveButton(R.string.ok, listener);
- dialog = builder.create();
- break;
- default:
- break;
- }
- Log.e("onCreateDialog", "onCreateDialog");
- return dialog;
- }
- }
【編輯推薦】
谷歌Android UI設(shè)計技巧:優(yōu)秀UI設(shè)計準(zhǔn)則
谷歌Android UI設(shè)計技巧:圖標(biāo)與指導(dǎo)說明
Android實戰(zhàn)系統(tǒng)對話框?qū)崿F(xiàn)登陸注冊功能
責(zé)任編輯:zhaolei
來源:
網(wǎng)絡(luò)轉(zhuǎn)載