2.5.2 使用alertdialog 创建列表对话框

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical" > <Button
android:id="@+id/btnColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="颜色列表" />
<Button
android:id="@+id/btnColorSC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="颜色单选" />
<Button
android:id="@+id/btnColorMC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="颜色多选" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView" /> </LinearLayout>
package com.moon.hello2;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.R.anim;
import android.R.bool;
import android.R.integer;
import android.animation.AnimatorSet.Builder;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.SimpleAdapter;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.SeekBar;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity {
private final String[] colorStrings = new String[]{"红色","绿色","黄色"};
private static final int DIALOG_SC = 1;
private final boolean[] checkStatus = new boolean[]{true, true, false};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.btnColor);
Button sc = (Button)findViewById(R.id.btnColorSC);
Button mc = (Button)findViewById(R.id.btnColorMC);
final TextView textView = (TextView)findViewById(R.id.textView1);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
android.app.AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("简单列表dialog");
builder.setIcon(R.drawable.ic_launcher);
builder.setItems(MainActivity.this.colorStrings, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int position) {
// TODO Auto-generated method stub
textView.setText("选择了"+MainActivity.this.colorStrings[position]);
}
});
builder.create().show();
}
});
sc.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MainActivity.this.showDialog(DIALOG_SC);
}
});
mc.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("这是一个多选alert");
builder.setIcon(R.drawable.ic_launcher);
builder.setMultiChoiceItems(MainActivity.this.colorStrings, MainActivity.this.checkStatus, new DialogInterface.OnMultiChoiceClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
String textString = "你选择了:";
for (int i = 0; i < MainActivity.this.checkStatus.length; i++) {
if (checkStatus[i]) {
textString += MainActivity.this.colorStrings[i]+",";
}
}
textView.setText(textString);
}
});
builder.setPositiveButton("确定", null);
builder.create().show();
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
@Deprecated
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if (DIALOG_SC == id) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setSingleChoiceItems(MainActivity.this.colorStrings, 1, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText("通过单选dialog选择了"+colorStrings[arg1]);
}
});
builder.setPositiveButton("确定", null);
return builder.create();
}
return super.onCreateDialog(id);
}
}
上一篇:使用YOLOv2进行图像检测


下一篇:[LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组