Android列表對(duì)話框用法實(shí)例分析
本文實(shí)例講述了Android列表對(duì)話框用法。分享給大家供大家參考。具體如下:
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="顯示列表對(duì)話框" android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
array.xml數(shù)組:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="hobby"> <item>游泳</item> <item>打籃球</item> <item>登山</item> </string-array> </resources>
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) {
// 顯示對(duì)話框
showDialog(DIALOG);
}
});
}
/**
* 創(chuàng)建列表對(duì)話框
*/
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog=null;
switch (id) {
case DIALOG:
Builder builder=new android.app.AlertDialog.Builder(this);
//設(shè)置對(duì)話框的圖標(biāo)
builder.setIcon(R.drawable.header);
//設(shè)置對(duì)話框的標(biāo)題
builder.setTitle("列表對(duì)話框");
//添加按鈕,android.content.DialogInterface.OnClickListener.OnClickListener
builder.setItems(R.array.hobby, new OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
String hoddy=getResources().getStringArray(R.array.hobby)[which];
editText.setText("您選擇了: "+hoddy);
}
});
//創(chuàng)建一個(gè)列表對(duì)話框
dialog=builder.create();
break;
}
return dialog;
}
}
運(yùn)行結(jié)果:

希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- Android之復(fù)選框?qū)υ捒蛴梅▽?shí)例分析
- Android普通對(duì)話框用法實(shí)例分析
- Android單選按鈕對(duì)話框用法實(shí)例分析
- Android之日期及時(shí)間選擇對(duì)話框用法實(shí)例分析
- android幾種不同對(duì)話框的實(shí)現(xiàn)方式
- Android開發(fā)必知 九種對(duì)話框的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- Android中創(chuàng)建一個(gè)透明的進(jìn)度對(duì)話框?qū)嵗?/a>
- Android復(fù)選框?qū)υ捒蛴梅▽?shí)例簡(jiǎn)析
相關(guān)文章
Android在kts中使用navigation及Args的方法
在Android項(xiàng)目中使用Kotlin腳本(kts)替代Groovy配置navigation和Args,需添加相關(guān)依賴,并在build.gradle中進(jìn)行配置,文章詳細(xì)介紹了如何在kts中使用navigation進(jìn)行頁(yè)面導(dǎo)航和參數(shù)傳遞,介紹了使用Bundle和Safe Args兩種方式安全傳遞參數(shù)2024-10-10
Android數(shù)據(jù)存儲(chǔ)之SQLite使用
SQLite是D.Richard Hipp用C語(yǔ)言編寫的開源嵌入式數(shù)據(jù)庫(kù)引擎。它支持大多數(shù)的SQL92標(biāo)準(zhǔn),并且可以在所有主要的操作系統(tǒng)上運(yùn)行2016-01-01
一行代碼教你解決Scrollview和TextInput焦點(diǎn)獲取問(wèn)題
這篇文章主要為大家介紹了一行代碼教你解決Scrollview和TextInput焦點(diǎn)獲取問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android自定義SeekBar實(shí)現(xiàn)視頻播放進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義SeekBar實(shí)現(xiàn)視頻播放進(jìn)度條的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android 程序執(zhí)行Linux命令的解決方法及注意事項(xiàng)
這篇文章主要介紹了Android 程序執(zhí)行LINUX命令的解決方法及注意事項(xiàng),本文通過(guò)問(wèn)題描述最終到解決方法,給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-12-12
Android SeekBar 自定義thumb旋轉(zhuǎn)動(dòng)畫效果
某些音樂(lè)播放或者視頻播放的界面上,資源還在加載時(shí),進(jìn)度條的原點(diǎn)(thumb)會(huì)顯示一個(gè)轉(zhuǎn)圈的效果。這篇文章主要介紹了Android SeekBar 自定義thumb thumb旋轉(zhuǎn)動(dòng)畫效果,需要的朋友可以參考下2021-11-11
Android頂部工具欄和底部工具欄的簡(jiǎn)單實(shí)現(xiàn)代碼
Android頂部工具欄和底部工具欄的簡(jiǎn)單實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-05-05

