android 單選、多選快顯功能表

來源:互聯網
上載者:User

標籤:

 

 

 

 

 

 

菜單單選視窗:

 

 

 

 

 

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
private String[] areas = new String[]{"全部","玉蘭香苑", "張江地鐵站", "金科路", "張江路", "紫薇路", "香楠小區" };
private boolean[] areaState=new boolean[]{true, false, false, false, false, false,false };
private RadioOnClick radioOnClick = new RadioOnClick(1);
private ListView areaCheckListView;
private ListView areaRadioListView;
private Button alertButton;
private Button checkBoxButton;
private Button radioButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

alertButton=(Button)findViewById(R.id.alertButton);
checkBoxButton=(Button)findViewById(R.id.checkBoxButton);
radioButton=(Button)findViewById(R.id.radioButton);

alertButton.setOnClickListener(new AlertClickListener());
checkBoxButton.setOnClickListener(new CheckBoxClickListener());
radioButton.setOnClickListener(new RadioClickListener());
}
/**
* 菜單快顯視窗
* @author xmz
*
*/
class AlertClickListener implements OnClickListener{
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this).setTitle("選擇地區").setItems(areas,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
Toast.makeText(MainActivity.this, "您已經選擇了: " + which + ":" + areas[which],Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}).show();
}
}
/**
* 多選框快顯功能表視窗
* @author xmz
*
*/
class CheckBoxClickListener implements OnClickListener{
@Override
public void onClick(View v) {
AlertDialog ad = new AlertDialog.Builder(MainActivity.this)
.setTitle("選擇地區")
.setMultiChoiceItems(areas,areaState,new DialogInterface.OnMultiChoiceClickListener(){
public void onClick(DialogInterface dialog,int whichButton, boolean isChecked){
//點擊某個地區
}
}).setPositiveButton("確定",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int whichButton){
String s = "您選擇了:";
for (int i = 0; i < areas.length; i++){
if (areaCheckListView.getCheckedItemPositions().get(i)){
s += i + ":"+ areaCheckListView.getAdapter().getItem(i)+ " ";
}else{
areaCheckListView.getCheckedItemPositions().get(i,false);
}
}
if (areaCheckListView.getCheckedItemPositions().size() > 0){
Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
}else{
//沒有選擇
}
dialog.dismiss();
}
}).setNegativeButton("取消", null).create();
areaCheckListView = ad.getListView();
ad.show();
}
}

/**
* 單選快顯功能表視窗
* @author xmz
*
*/
class RadioClickListener implements OnClickListener {
@Override
public void onClick(View v) {
AlertDialog ad =new AlertDialog.Builder(MainActivity.this).setTitle("選擇地區")
.setSingleChoiceItems(areas,radioOnClick.getIndex(),radioOnClick).create();
areaRadioListView=ad.getListView();
ad.show();
}
}
/**
* 點擊單選框事件
* @author xmz
*
*/
class RadioOnClick implements DialogInterface.OnClickListener{
private int index;

public RadioOnClick(int index){
this.index = index;
}
public void setIndex(int index){
this.index=index;
}
public int getIndex(){
return index;
}

public void onClick(DialogInterface dialog, int whichButton){
setIndex(whichButton);
Toast.makeText(MainActivity.this, "您已經選擇了: " + index + ":" + areas[index], Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}
}

 

 

 

<?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"
>
<Button
android:id="@+id/alertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="菜單選擇視窗"
/>
<Button
android:id="@+id/checkBoxButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多選菜單選擇視窗"
/>
<Button
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="單選菜單選擇視窗-1"
/>
</LinearLayout>

android 單選、多選快顯功能表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.