package com.example.chadfirstandroidapp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.*; import android.view.View; import android.app.AlertDialog; import android.content.DialogInterface; public class MainActivity extends Activity { private TextView MessageTextView=null; private int singleChoicedIndex=0; private String[] hobby=new String[]{"游泳","羽毛球","跳绳","篮球"}; private boolean[] multiChoicedInfo=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MessageTextView=(TextView)findViewById(R.id.MessageTextView);
//普通对话框
Button normalButton=(Button)findViewById(R.id.normalButton); normalButton.setOnClickListener(new Button.OnClickListener(){ public void onClick(View view){ new AlertDialog.Builder(MainActivity.this) .setTitle("温馨提示") .setMessage("你的年龄过大!") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MessageTextView.setText("你承认你的年龄过大"); } }) .show(); } });
//确定对话框
Button confirmButton=(Button)findViewById(R.id.confirmButton); confirmButton.setOnClickListener(new Button.OnClickListener(){ public void onClick(View view){ new AlertDialog.Builder(MainActivity.this) .setTitle("温馨提示") .setMessage("你确定你太胖了吗?") .setPositiveButton("是",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MessageTextView.setText("你承认你太胖了"); } }) .setNegativeButton("否",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MessageTextView.setText("你没有承认你太胖了"); } }) .show(); } });
//输入对话框
Button inputButton=(Button)findViewById(R.id.InputButton); inputButton.setOnClickListener(new Button.OnClickListener(){ public void onClick(View view){ final EditText editText=new EditText(MainActivity.this); new AlertDialog.Builder(MainActivity.this) .setTitle("请输入内容") .setIcon(android.R.drawable.ic_dialog_info) .setView(editText) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MessageTextView.setText(editText.getText()); } }) .setNegativeButton("取消", null) .show(); } });
//单选对话框
Button singleChoiceButton=(Button)findViewById(R.id.singleChoiceButton); singleChoiceButton.setOnClickListener(new Button.OnClickListener(){ public void onClick(View view){ new AlertDialog.Builder(MainActivity.this) .setTitle("请选择你的爱好") .setIcon(android.R.drawable.ic_dialog_info) .setSingleChoiceItems(hobby , 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { singleChoicedIndex=which; } }) .setPositiveButton("确定",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MessageTextView.setText(hobby[singleChoicedIndex]); } }) .show(); } });
//多选对话框
Button multiChoiceButton=(Button)findViewById(R.id.multiChoiceButton); multiChoiceButton.setOnClickListener(new Button.OnClickListener(){ public void onClick(View view){ multiChoicedInfo=new boolean[hobby.length]; new AlertDialog.Builder(MainActivity.this) .setTitle("请选择你的爱好") .setIcon(android.R.drawable.ic_dialog_info) .setMultiChoiceItems(hobby, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if(isChecked){ multiChoicedInfo[which]=true; }else{ multiChoicedInfo[which]=false; } } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String choicedHobby=""; for(int i=0;i<multiChoicedInfo.length;i++){ if(multiChoicedInfo[i]){ choicedHobby+=hobby[i]+","; } } MessageTextView.setText(choicedHobby); } }) .show(); } });
//列表对话框
Button listingButton=(Button)findViewById(R.id.listingButton); listingButton.setOnClickListener(new Button.OnClickListener(){ public void onClick(View view){ new AlertDialog.Builder(MainActivity.this) .setTitle("请选择你的爱好") .setIcon(android.R.drawable.ic_dialog_info) .setItems(hobby, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MessageTextView.setText(hobby[which]); } }) .show(); } });
//图片对话框
Button imageButton=(Button)findViewById(R.id.imageButton); imageButton.setOnClickListener(new Button.OnClickListener(){ public void onClick(View view){ ImageView imageView=new ImageView(MainActivity.this); imageView.setImageResource(android.R.drawable.ic_dialog_email); new AlertDialog.Builder(MainActivity.this) .setTitle("图片框") .setView(imageView) .setPositiveButton("确定",null) .show(); } }); }
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }