Android單選按鈕對話框用法實例分析
更新時間:2015年09月15日 11:28:43 作者:Ruthless
這篇文章主要介紹了Android單選按鈕對話框用法,以完整實例形式分析布局及對話框類的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了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>
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) { // 顯示對話框 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("單選按鈕對話框"); //0: 默認第一個單選按鈕被選中 builder.setSingleChoiceItems(R.array.hobby, 0, new OnClickListener(){ public void onClick(DialogInterface dialog, int which) { String hoddy=getResources().getStringArray(R.array.hobby)[which]; editText.setText("您選擇了: "+hoddy); } }); //添加一個確定按鈕 builder.setPositiveButton(" 確 定 ", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) { } }); //創(chuàng)建一個單選按鈕對話框 dialog=builder.create(); break; } return dialog; } }
運行結(jié)果:
希望本文所述對大家的Android程序設(shè)計有所幫助。
相關(guān)文章
Android應用開發(fā)中控制反轉(zhuǎn)IoC設(shè)計模式使用教程
這篇文章主要介紹了Android應用開發(fā)中控制反轉(zhuǎn)IoC設(shè)計模式使用教程,IoC其實更常被理解為一種依賴注入的模式,用來分解業(yè)務(wù)層降低耦合,需要的朋友可以參考下2016-04-04Android App在ViewPager中使用Fragment的實例講解
這篇文章主要介紹了Android App在ViewPager中使用Fragment的實例講解,ViewPager組件主要被用來制作滑動切換效果,需要的朋友可以參考下2016-03-03Android音視頻開發(fā)之MediaPlayer使用教程
Android多媒體框架支持播放提供了MediaPlayerAPI,可以通過MediaPlayer來實現(xiàn)媒體文件播放??梢哉fMediaPlayer是非常方便使用的多媒體播放器。本文將詳細講解MediaPlayer的使用,需要的可以參考一下2022-04-04ViewPager+Fragment實現(xiàn)側(cè)滑導航欄
這篇文章主要為大家詳細介紹了ViewPager+Fragment實現(xiàn)側(cè)滑導航欄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05Android 實現(xiàn)IOS 滾輪選擇控件的實例(源碼下載)
這篇文章主要介紹了 Android 實現(xiàn)IOS 滾輪選擇控件的實例(源碼下載)的相關(guān)資料,需要的朋友可以參考下2017-03-03