當前位置:首頁 » 安卓系統 » android自定義樣式的對話框

android自定義樣式的對話框

發布時間: 2025-08-09 18:35:20

❶ android 自定義的dialog,edit text 不能獲得焦點,彈出軟鍵盤。

1、首先自定義布局
2、把自定義布局放入dialog中顯示
3、通過自定義布局查找對應的edittext組件
final View DialogView = a .inflate ( R.layout.loand, null);//1、自定義布局
//創建對話框
AlertDialog dlg = new AlertDialog.Builder(loand.this)
.setTitle("登錄框")
.setView(DialogView)//設置自定義對話框的樣式,2、自定義布局放入dialog中顯示
.setPositiveButton("登陸", //設置"確定"按鈕
new DialogInterface.OnClickListener() //設置事件監聽{
public void onClick(DialogInterface dialog, int whichButton){editText1 =(EditText) DialogView.findViewById(R.id.editText1);
editText2 =(EditText) DialogView.findViewById(R.id.editText2);//3、過自定義布局查找對應的edittext組件
String id = editText1.getText().toString();
String password = editText2.getText().toString();

❷ Android UI開發中,設置Activity的什麼屬性可以使Activity顯示為對話框樣式.

將activity設置成對話框樣式,只需在activity屬性裡面增加下面一句代碼:

❸ android自定義的對話框怎麼調用

Android自定義對話框的思路就是編寫對話框的布局文件xml,然後在對話框中顯示不同的控制項。以下以顯示文本控制項為例(ImageView等都可以顯示)。
1.布局文件connect_dlg.xml(比如http://www.tiecou.com/)
<?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="fill_parent"
android:background="#ffffffff"
android:orientation="vertical"
android:id="@+id/llToast" >
<TextView
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:textColor="#ffffffff"
android:layout_width="fill_parent"
android:gravity="center"
android:textSize="16sp"
android:background="#FF129de2"
android:id="@+id/tvTitleToast" />
<LinearLayout
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/llToastContent"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_marginBottom="1dip"
android:layout_width="wrap_content"
android:padding="15dip"
android:background="#FFFFFFFF" >
<TextView
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:paddingLeft="10dip"
android:layout_width="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:textColor="#FFff6699"
android:id="@+id/tvTextToast" />
</LinearLayout>
<LinearLayout
android:id="@+id/MyLayout_ad2"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="40sp">
<com.tencent.exmobwin.banner.TAdView
android:id="@+id/adview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top|right" >
</com.tencent.exmobwin.banner.TAdView>
</LinearLayout>
</LinearLayout>
2.編寫顯示對話框函數。ShowConnectDialog(String textString)
private void ShowConnectDialog(String textString) {
LinearLayout loginLayout1 = (LinearLayout) getLayoutInflater().inflate(
R.layout.connect_dlg, null);
// adView.
TextView title = (TextView) loginLayout1
.findViewById(R.id.tvTitleToast);
title.setText("系統提示");
TextView text1 = (TextView) loginLayout1.findViewById(R.id.tvTextToast);
text1.setText(textString);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(loginLayout1);
builder.setPositiveButton("下載MobCtrl伺服器?", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//處理確定按鈕
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 處理取消按鈕
finish();
}
});
builder.create().show();
}
3.顯示對話框。在需要顯示的地方調用即可。
ShowConnectDialog("連接超時,請檢查伺服器是否開啟及IP地址是否輸入正確。確保電腦和手機連接在同一個網路內。");

❹ android 的 AlertDialog 對話框樣式可以修改嗎

Android 提供了AlertDialog類可通過其內部類Builder輕松創建對話框窗口,但是沒法對這個對話框窗口進行定製,為了修改 AlertDialog 窗口顯示的外觀,解決的辦法就是創建一個指定的 AlertDialog 和 AlertDialog.Builder 類。

定義外觀

我們希望將上面默認的對話框外觀修改為如下圖所示的新對話框風格:

編寫對話框和 Builder 類


❺ android中的對話框怎麼寫

Activities提供了一種方便管理的創建、保存、回復的對話框機制,例如onCreateDialog(int),onPrepareDialog(int,Dialog),showDialog(int),dismissDialog(int)等方法,如果使用這些方法的話,Activity將通過getOwnerActivity()方法返回該Activity管理的對話框(dialog).

onCreateDialog(int):當你使用這個回調函數時,Android系統會有效的設置這個Activity為每個對話框的所有者,從而自動管理每個對話框的狀態並掛靠到Activity上。這樣,每個對話框繼承這個Activity的特定屬性。比如,當一個對話框打開時,菜單鍵顯示為這個Activity定義的選項菜單,音量鍵修改Activity使用的音頻流。

showDialog(int):當你想要顯示一個對話框時,調用showDialog(intid)方法並傳遞一個唯一標識這個對話框的整數。當對話框第一次被請求時,Android從你的Activity中調用onCreateDialog(intid),你應該在這里初始化這個對話框Dialog。這個回調方法被傳以和showDialog(intid)相同的ID。當你創建這個對話框後,在Activity的最後返回這個對象。

onPrepareDialog(int,Dialog):在對話框被顯示之前,Android還調用了可選的回調函數onPrepareDialog(intid,Dialog).如果你想在每一次對話框被打開時改變它的任何屬性,你可以定義這個方法。這個方法在每次打開對話框時被調用,而onCreateDialog(int)僅在對話框第一次打開時被調用。如果你不定義onPrepareDialog(),那麼這個對話框將保持和上次打開時一樣。這個方法也被傳遞以對話框的ID,和在onCreateDialog()中創建的對話框對象。

dismissDialog(int):當你准備關閉對話框時,你可以通過對這個對話框調用dismiss()來消除它。如果需要,你還可以從這個Activity中調用dismissDialog(intid)方法,這實際上將為你對這個對話框調用dismiss()方法。如果你想使用onCreateDialog(intid)方法來管理你對話框的狀態(就如同在前面的章節討論的那樣),然後每次你的對話框消除的時候,這個對話框對象的狀態將由該Activity保留。如果你決定不再需要這個對象或者清除該狀態是重要的,那麼你應該調用removeDialog(intid)。這將刪除任何內部對象引用而且如果這個對話框正在顯示,它將被消除。

下面是幾種對話框的效果

圖一:

圖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 = 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類型

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

圖4效果:信息內容是一組單選框

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

圖5效果:信息內容是一組多選框

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效果:信息內容是一組簡單列表項

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

圖7效果:信息內容是一個自定義的布局

1.布局文件

<?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>

2.調用代碼

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();

❻ android如何創建帶3個按鈕的對話框

1.先在布局界面上,拖進來一個按鈕控制項,並設置顯示的文字,記得保存(Ctrl+S)

之後在代碼界面上定義該按鈕。

2.新建一個按鈕點擊的方法。
onClick(View v) :點擊之後的動作。

3.設置按鈕的點擊事件指向我們新建的點擊方法。
setOnClickListener:設置點擊之後觸發的動作。

4.在onClick里添加彈出對話框的代碼。
AlertDialog:一個對話框類。
MainActivity.this:對話框顯示的位置。
setTitle:設置標題。
setMessage:設置內容。
setPositiveButton:設置對話框的按鈕。
show():顯示對話框。

至此所有代碼已經完成,編譯並生成,在Android安卓虛擬機上顯示如下。

❼ 怎麼使用android軟體的搜索功能

當你需要在你的應用程序中提供搜索服務時,通過使用Android的搜索框架,應用程序將顯示一個自定義搜索對話框來處理用戶的搜索請求。通過一個簡單的搜索按鈕或從您的應用程序中調用API,搜索對話框就會顯示在屏幕的頂部,並會自動顯示應用程序圖標。

本文將教你如何為你的應用程序提供一個自定義搜索對話框。這樣做,給您的用戶提供一個標准化的搜索體驗,並能增加如語音搜索和搜索建議等功能。

基礎知識

Android的搜索框架將代您管理的搜索對話框,您不需要自己去開發一個搜索框,不需要擔心要把搜索框放什麼位置,也不需要擔心搜索框影響您當前的界面。所有的這些工作都由SearchManager類來為您處理(以下簡稱「搜索管理器」),它管理的Android搜索對話框的整個生命周期,並執行您的應用程序將發送的搜索請求,返回相應的搜索關鍵字。

當用戶執行一個搜索,搜索管理器將使用一個專門的Intent把搜索查詢的關鍵字傳給您在配置文件中配置的處理搜索結果的Activity。從本質上講,所有你需要的就是一個Activity來接收Intent,然後執行搜索,並給出結果。具體來說,你需要的做的事就包括以下內容:

一個搜索配置
我們用個XML配置文件來對搜索對話框進行配置,包括一些功能的配置,如文本框,設置語音搜索和搜索建議中顯示的提示文字等。

一個用來處理搜索請求的Activity
這個Activity用來接收搜索查詢的內容,然後搜索您的數據並顯示搜索結果。

一種用戶執行搜索的途徑
默認情況下,一旦你配置了一個可搜索的Activity,設備搜索鍵(如果有)將調用搜索對話框。然而,你應該始終提供另一種手段,讓用戶可以調用搜索對話框,如在選項菜單中的搜索按鈕或其他用戶界面上的按鈕,因為不是所有的設備提供一個專門的搜索鍵。

創建一個搜索對話框配置文件

搜索框配置文件是一個用來配置您的應用程序中搜索框的設置的XML文件,這個文件一般命名為searchable.xml,並且必須保存在項目的res/xml/目錄下。

配置文件的根節點必須為,可以有一個或多個屬性。

熱點內容
編譯安裝boost 發布:2025-08-31 01:45:04 瀏覽:36
sql2008數據還原 發布:2025-08-31 01:45:00 瀏覽:889
電視廣告腳本範文 發布:2025-08-31 01:33:10 瀏覽:453
p2p網貸系統源碼下載 發布:2025-08-31 01:10:34 瀏覽:905
在線聊天室源碼 發布:2025-08-31 01:10:33 瀏覽:62
python入門知乎 發布:2025-08-31 00:58:06 瀏覽:202
浪潮全快閃記憶體儲 發布:2025-08-31 00:56:35 瀏覽:741
外部存儲分析 發布:2025-08-31 00:49:36 瀏覽:252
2棟3單元會用什麼wifi密碼 發布:2025-08-31 00:47:45 瀏覽:329
線切割機床編程 發布:2025-08-31 00:42:31 瀏覽:859