Android之復(fù)選框?qū)υ捒蛴梅▽嵗治?/h1>
更新時間:2015年09月15日 11:18:19 作者:Ruthless
這篇文章主要介紹了Android之復(fù)選框?qū)υ捒蛴梅?涉及Android頁面布局、對話框類等相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Android之復(fù)選框?qū)υ捒蛴梅ā7窒斫o大家供大家參考。具體如下:
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="顯示復(fù)選框?qū)υ捒?
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>
AlertActivity類
package com.ljq.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
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;
boolean[] flags=new boolean[]{false,false,false};//初始復(fù)選情況
String[] items=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
items=getResources().getStringArray(R.array.hobby);
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)建復(fù)選框?qū)υ捒?
*/
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog=null;
switch (id) {
case DIALOG:
Builder builder=new android.app.AlertDialog.Builder(this);
//設(shè)置對話框的圖標(biāo)
builder.setIcon(R.drawable.header);
//設(shè)置對話框的標(biāo)題
builder.setTitle("復(fù)選框?qū)υ捒?);
builder.setMultiChoiceItems(R.array.hobby, flags, new DialogInterface.OnMultiChoiceClickListener(){
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flags[which]=isChecked;
String result = "您選擇了:";
for (int i = 0; i < flags.length; i++) {
if(flags[i]){
result=result+items[i]+"、";
}
}
editText.setText(result.substring(0, result.length()-1));
}
});
//添加一個確定按鈕
builder.setPositiveButton(" 確 定 ", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
});
//創(chuàng)建一個復(fù)選框?qū)υ捒?
dialog=builder.create();
break;
}
return dialog;
}
}
運行結(jié)果:

希望本文所述對大家的Android程序設(shè)計有所幫助。
您可能感興趣的文章:- Android RadioButton單選框的使用方法
- Android程序開發(fā)中單選按鈕(RadioGroup)的使用詳解
- Android中創(chuàng)建對話框(確定取消對話框、單選對話框、多選對話框)實例代碼
- Android自定義單選多選下拉列表的實例代碼
- Android單選按鈕對話框用法實例分析
- android實現(xiàn)單選按鈕功能
- Android ListView實現(xiàn)單選及多選等功能示例
- Android中CheckBox復(fù)選框控件使用方法詳解
- Android復(fù)選框?qū)υ捒蛴梅▽嵗單?/a>
- Android開發(fā)之獲取單選與復(fù)選框的值操作示例
相關(guān)文章
-
Android Studio 恢復(fù)小窗口停靠模式(Docked Mode)
這篇文章主要介紹了Android Studio 恢復(fù)小窗口??磕J?Docked Mode),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧 2020-04-04
-
Android使用Scrolling機制實現(xiàn)Tab吸頂效果
app 首頁中經(jīng)常要實現(xiàn)首頁頭卡共享,tab 吸頂,內(nèi)容區(qū)通過 ViewPager 切換的需求,以前往往是利用事件處理來完成,但是這些也有一定的弊端和滑動方面不如意的地方,本文我們利用NestedScrolling機制來實現(xiàn),感興趣的朋友可以參考下 2024-01-01
-
Android 為ListView添加分段標(biāo)頭的方法
下面小編就為大家?guī)硪黄狝ndroid 為ListView添加分段標(biāo)頭的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧 2017-04-04
-
Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路
這篇文章主要介紹了Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路的相關(guān)資料,在開發(fā)網(wǎng)路狀態(tài)的時候需要先判斷是否開啟之后在提示用戶進行開啟操作,需要的朋友可以參考下 2017-08-08
-
基于Android studio3.6的JNI教程之helloworld思路詳解
這篇文章主要介紹了基于Android studio3.6的JNI教程之helloworld,本文通過圖文實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下 2020-03-03
最新評論
本文實例講述了Android之復(fù)選框?qū)υ捒蛴梅ā7窒斫o大家供大家參考。具體如下:
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="顯示復(fù)選框?qū)υ捒? 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>
AlertActivity類
package com.ljq.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
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;
boolean[] flags=new boolean[]{false,false,false};//初始復(fù)選情況
String[] items=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
items=getResources().getStringArray(R.array.hobby);
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)建復(fù)選框?qū)υ捒?
*/
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog=null;
switch (id) {
case DIALOG:
Builder builder=new android.app.AlertDialog.Builder(this);
//設(shè)置對話框的圖標(biāo)
builder.setIcon(R.drawable.header);
//設(shè)置對話框的標(biāo)題
builder.setTitle("復(fù)選框?qū)υ捒?);
builder.setMultiChoiceItems(R.array.hobby, flags, new DialogInterface.OnMultiChoiceClickListener(){
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flags[which]=isChecked;
String result = "您選擇了:";
for (int i = 0; i < flags.length; i++) {
if(flags[i]){
result=result+items[i]+"、";
}
}
editText.setText(result.substring(0, result.length()-1));
}
});
//添加一個確定按鈕
builder.setPositiveButton(" 確 定 ", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
});
//創(chuàng)建一個復(fù)選框?qū)υ捒?
dialog=builder.create();
break;
}
return dialog;
}
}
運行結(jié)果:

希望本文所述對大家的Android程序設(shè)計有所幫助。
您可能感興趣的文章:
- Android RadioButton單選框的使用方法
- Android程序開發(fā)中單選按鈕(RadioGroup)的使用詳解
- Android中創(chuàng)建對話框(確定取消對話框、單選對話框、多選對話框)實例代碼
- Android自定義單選多選下拉列表的實例代碼
- Android單選按鈕對話框用法實例分析
- android實現(xiàn)單選按鈕功能
- Android ListView實現(xiàn)單選及多選等功能示例
- Android中CheckBox復(fù)選框控件使用方法詳解
- Android復(fù)選框?qū)υ捒蛴梅▽嵗單?/a>
- Android開發(fā)之獲取單選與復(fù)選框的值操作示例
相關(guān)文章
Android Studio 恢復(fù)小窗口停靠模式(Docked Mode)
這篇文章主要介紹了Android Studio 恢復(fù)小窗口??磕J?Docked Mode),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Android使用Scrolling機制實現(xiàn)Tab吸頂效果
app 首頁中經(jīng)常要實現(xiàn)首頁頭卡共享,tab 吸頂,內(nèi)容區(qū)通過 ViewPager 切換的需求,以前往往是利用事件處理來完成,但是這些也有一定的弊端和滑動方面不如意的地方,本文我們利用NestedScrolling機制來實現(xiàn),感興趣的朋友可以參考下2024-01-01
Android 為ListView添加分段標(biāo)頭的方法
下面小編就為大家?guī)硪黄狝ndroid 為ListView添加分段標(biāo)頭的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路
這篇文章主要介紹了Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路的相關(guān)資料,在開發(fā)網(wǎng)路狀態(tài)的時候需要先判斷是否開啟之后在提示用戶進行開啟操作,需要的朋友可以參考下2017-08-08
基于Android studio3.6的JNI教程之helloworld思路詳解
這篇文章主要介紹了基于Android studio3.6的JNI教程之helloworld,本文通過圖文實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

