Android普通對話框用法實例分析
更新時間:2015年09月16日 12:06:53 作者:Ruthless
這篇文章主要介紹了Android普通對話框用法,以實例形式較為詳細的分析了Android對話框的創(chuàng)建技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Android普通對話框用法。分享給大家供大家參考。具體如下:
main.xml布局文件:
<?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="fill_parent"> <EditText android:text="" android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> <Button android:text="顯示普通對話框" android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
AlertDialog類:
package com.ljq.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AlertDialog extends Activity {
private EditText editText;
private final static int DIALOG=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText=(EditText)findViewById(R.id.editText);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 顯示對話框
showDialog(DIALOG);
}
});
}
/**
* 創(chuàng)建普通對話框
*/
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog=null;
switch (id) {
case DIALOG:
Builder builder=new android.app.AlertDialog.Builder(this);
//設(shè)置對話框的圖標
builder.setIcon(R.drawable.header);
//設(shè)置對話框的標題
builder.setTitle("普通對話框");
//設(shè)置對話框的顯示內(nèi)容
builder.setMessage("這是普通對話框中的內(nèi)容!!");
//添加按鈕,android.content.DialogInterface.OnClickListener.OnClickListener
builder.setPositiveButton(" 確 定 ", new OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
editText.setText("這是普通對話框中的內(nèi)容??!");
}
});
//創(chuàng)建一個普通對話框
dialog=builder.create();
break;
}
return dialog;
}
}
運行結(jié)果:

希望本文所述對大家的Android程序設(shè)計有所幫助。
相關(guān)文章
android輕量級無侵入式管理數(shù)據(jù)庫自動升級組件
這篇文章主要為大家介紹了android輕量級無侵入式管理數(shù)據(jù)庫自動升級組件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
OpenHarmony如何調(diào)用電話服務(wù)API撥打電話
OpenHarmony3.1版本標準系統(tǒng)增加了通話相關(guān)的聯(lián)系人應用,來電應用等,在系統(tǒng)服務(wù)層面電話相關(guān)功能也比較完善,這篇文章主要介紹了OpenHarmony如何調(diào)用電話服務(wù)API撥打電話2022-11-11
Android編程實現(xiàn)根據(jù)經(jīng)緯度查詢地址并對獲取的json數(shù)據(jù)進行解析的方法
這篇文章主要介紹了Android編程實現(xiàn)根據(jù)經(jīng)緯度查詢地址并對獲取的json數(shù)據(jù)進行解析的方法,結(jié)合實例形式分析了Android的經(jīng)緯度地址解析與json格式數(shù)據(jù)操作相關(guān)技巧,需要的朋友可以參考下2017-02-02
使用AndroidStudio上傳忽略文件至SVN Server的解決辦法
這篇文章主要介紹了使用AndroidStudio上傳忽略文件至SVN Server的解決辦法 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06
Android使用原生組件WebView加載網(wǎng)頁和數(shù)據(jù)的方法
這篇文章主要介紹了Android使用原生組件WebView加載網(wǎng)頁和數(shù)據(jù)的方法的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android Volley擴展實現(xiàn)支持進度條的文件上傳功能
這篇文章主要為大家詳細介紹了Android Volley擴展實現(xiàn)文件上傳與下載功能,支持進度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12

