欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android單選按鈕對(duì)話框用法實(shí)例分析

 更新時(shí)間:2015年09月15日 11:28:43   作者:Ruthless  
這篇文章主要介紹了Android單選按鈕對(duì)話框用法,以完整實(shí)例形式分析布局及對(duì)話框類的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(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ì)話框");
      //0: 默認(rèn)第一個(gè)單選按鈕被選中
      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);
        }
      });
      //添加一個(gè)確定按鈕
      builder.setPositiveButton(" 確 定 ", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int which) {
        }
      });
      //創(chuàng)建一個(gè)單選按鈕對(duì)話框
      dialog=builder.create();
      break;
    }
    return dialog;
  }
}

運(yùn)行結(jié)果:

希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論