当前位置: 代码迷 >> Android >> android 各种惯用,不易记的dialog,等控件整理
  详细解决方案

android 各种惯用,不易记的dialog,等控件整理

热度:41   发布时间:2016-05-01 14:59:34.0
android 各种常用,不易记的dialog,等控件整理


package com.su.testalldialog;import java.util.ArrayList;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.Spinner;import android.widget.Toast;public class TestAllDialogActivity extends Activity {	String[] weekdays;	Context context;	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		context = this;		weekdays = new String[] { "Sun", "Mon", "Tue", "Wed" };		Button button1 = (Button) findViewById(R.id.button1);		Button button2 = (Button) findViewById(R.id.button2);		Button button3 = (Button) findViewById(R.id.button3);		Button button4 = (Button) findViewById(R.id.button4);		Button button5 = (Button) findViewById(R.id.button5);		Button button6 = (Button) findViewById(R.id.button6);		button6.setOnClickListener(listener);		button5.setOnClickListener(listener);		button1.setOnClickListener(listener);		button2.setOnClickListener(listener);		button3.setOnClickListener(listener);		button4.setOnClickListener(listener);		initSpinner();		initAutocompletetextview();	}	private void initAutocompletetextview() {		AutoCompleteTextView autocompletetextview = (AutoCompleteTextView) findViewById(R.id.autocompletetextview);		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,				android.R.layout.simple_spinner_dropdown_item, weekdays);// 实现简单的adapter		autocompletetextview.setAdapter(adapter);	}	private void initSpinner() {		Spinner spinner = (Spinner) findViewById(R.id.spinner);		spinner.setOnItemSelectedListener(slistener);		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,				android.R.layout.simple_spinner_item, weekdays);// 实现简单的adapter		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 实现选项的样式		spinner.setAdapter(adapter);	}	private OnItemSelectedListener slistener = new OnItemSelectedListener() {		@Override		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,				long arg3) {			toast(weekdays[arg2]);		}		@Override		public void onNothingSelected(AdapterView<?> arg0) {			// TODO Auto-generated method stub		}	};	int index = 0;// 用来标记哪一项被选择	private OnClickListener listener = new OnClickListener() {		@Override		public void onClick(View v) {			Builder dialog = new AlertDialog.Builder(context);			switch (v.getId()) {			case R.id.button1:				dialog.setIcon(R.drawable.ic_launcher);				dialog.setTitle("简单选择对话框");				dialog.setItems(weekdays,						new DialogInterface.OnClickListener() {							@Override							public void onClick(DialogInterface dialog,									int which) {								toast(weekdays[which]);							}						});				dialog.show();				break;			case R.id.button2:				dialog.setIcon(R.drawable.ic_launcher);				dialog.setTitle("单选对话框");				dialog.setSingleChoiceItems(weekdays, 1,						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog,									int which) {								index = which;							}						});				dialog.setPositiveButton("是",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {								toast(weekdays[index]);							}						});				dialog.setNegativeButton("否",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {							}						});				dialog.show();				break;			case R.id.button3:				final ArrayList<String> multiWeekadys = new ArrayList<String>();// 选择的数据放到list中				dialog.setIcon(R.drawable.ic_launcher);				dialog.setTitle("复选对话框");				final boolean[] checkeditems = new boolean[weekdays.length];// 设置一打开时的选择情况				dialog.setMultiChoiceItems(weekdays, checkeditems,						new DialogInterface.OnMultiChoiceClickListener() {							@Override							public void onClick(DialogInterface dialog,									int which, boolean isChecked) {								if (isChecked) {									checkeditems[which] = true;// 获取一个被选中的列表								}							}						});				dialog.setPositiveButton("是",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {								for (int j = 0; j < checkeditems.length; j++) {// 遍历一遍看那个是选中的									if (checkeditems[j]) {										multiWeekadys.add(weekdays[j]);									}								}								toast(multiWeekadys.toString());							}						});				dialog.setNegativeButton("否",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {							}						});				dialog.show();				break;			case R.id.button4:				dialog.setIcon(R.drawable.ic_launcher);				dialog.setTitle("简单警告");				dialog.setMessage("测试警告");				dialog.setPositiveButton("是",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {								toast("是");							}						});				dialog.setNeutralButton("中",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {								toast("中");							}						});				dialog.setNegativeButton("否",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {							}						});				dialog.show();				break;			case R.id.button5:				final EditText et = new EditText(context);				et.setText("测试");				dialog.setIcon(R.drawable.ic_launcher);				dialog.setTitle("简单警告");				dialog.setMessage("测试警告");				dialog.setView(et);				dialog.setPositiveButton("是",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {								toast(et.getText().toString());							}						});				dialog.setNeutralButton("中",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {								toast("中");							}						});				dialog.setNegativeButton("否",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {							}						});				dialog.show();				break;			case R.id.button6:				final ImageView im = new ImageView(context);				im.setImageResource(R.drawable.ic_launcher);				dialog.setIcon(R.drawable.ic_launcher);				dialog.setTitle("简单警告");				dialog.setMessage("测试警告");				dialog.setView(im);				dialog.setPositiveButton("是",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {							}						});				dialog.setNeutralButton("中",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {								toast("中");							}						});				dialog.setNegativeButton("否",						new DialogInterface.OnClickListener() {							public void onClick(DialogInterface dialog, int i) {							}						});				dialog.show();				break;			default:				break;			}		}	};	private void toast(String msg) {		Toast.makeText(context, msg, 10).show();	}}


  相关解决方案