文件名称:安卓开发对话框大全
文件大小:9KB
文件格式:TXT
更新时间:2018-12-13 12:48:05
对话框
package com.tarena.day09_alertdialog; import java.util.Calendar; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.ProgressDialog; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.content.DialogInterface; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity { private AlertDialog dialog1; private AlertDialog dialog2; private AlertDialog dialog3; private AlertDialog dialog4; private AlertDialog dialog5; private ProgressDialog dialog6; private TimePickerDialog dialog7; private DatePickerDialog dialog8; private EditText etUserName, etPassword; private boolean isFlag; private void initTimePickerDialog(){ dialog7 = new TimePickerDialog(this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(MainActivity.this, "Time:"+hourOfDay+":"+minute, Toast.LENGTH_LONG).show(); } }, 0, 0, false); } private void initDatePickerDialog(){ dialog8 = new DatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(MainActivity.this, "Date:"+year+":"+(monthOfYear+1)+":"+dayOfMonth, Toast.LENGTH_LONG).show(); } }, 2015, Calendar.SEPTEMBER, 17); } private void initAlertDialog1() { Builder builder = new Builder(this); // AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); // 添加显示信息 builder.setMessage("最简单对话框"); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); // 创建AlertDialog对象 dialog1 = builder.create(); } private void initAlertDialog2() { Builder builder = new Builder(this); builder.setTitle("提示"); // 添加显示信息 builder.setMessage("最简单对话框"); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); // 设置确定按钮 builder.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show(); } }); // 设置取消按钮 builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog2.dismiss(); } }); // 创建AlertDialog对象 dialog2 = builder.create(); } private void initAlertDialog3() { Builder builder = new Builder(this); builder.setTitle("提示"); // 设置控件方法 builder.setView(loadView()); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); // 设置确定按钮 builder.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String username = etUserName.getText().toString(); String password = etPassword.getText().toString(); Toast.makeText( MainActivity.this, "UserName:" + username + ",Password:" + password, Toast.LENGTH_SHORT).show(); } }); // 设置取消按钮 builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog3.dismiss(); } }); // 创建AlertDialog对象 dialog3 = builder.create(); } private void initAlertDialog4() { Builder builder = new Builder(this); builder.setTitle("提示"); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); builder.setSingleChoiceItems(new String[] { "item1", "item2", "item3", "item4" }, 0, new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: Toast.makeText(MainActivity.this, "item1", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(MainActivity.this, "item2", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(MainActivity.this, "item3", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(MainActivity.this, "item4", Toast.LENGTH_SHORT).show(); break; default: break; } } }); // 创建AlertDialog对象 dialog4 = builder.create(); } private void initAlertDialog5(){ Builder builder = new Builder(this); builder.setTitle("提示"); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); builder.setMultiChoiceItems(new String[]{"item1","item2","item3","item4"}, new boolean[]{true,false,true,false}, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(MainActivity.this, "Which:"+which+",IsChechked:"+isChecked, Toast.LENGTH_SHORT).show(); } }); dialog5 = builder.create(); } /** * 加载布局 * * @return */ private View loadView() { LayoutInflater inflater = LayoutInflater.from(this); // LayoutInflater inflater = (LayoutInflater) // getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.login, null); etUserName = (EditText) view.findViewById(R.id.et_username); etPassword = (EditText) view.findViewById(R.id.et_password); return view; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initAlertDialog1(); initAlertDialog2(); initAlertDialog3(); initAlertDialog4(); initAlertDialog5(); initTimePickerDialog(); initDatePickerDialog(); addListener(); } private void addListener() { this.findViewById(R.id.button1).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog1.show(); } }); this.findViewById(R.id.button2).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog2.show(); } }); this.findViewById(R.id.button3).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog3.show(); } }); this.findViewById(R.id.button4).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog4.show(); } }); this.findViewById(R.id.button5).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog5.show(); } }); this.findViewById(R.id.button6).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { isFlag = true; dialog6 = new ProgressDialog(MainActivity.this); dialog6.setTitle("提示"); dialog6.setIcon(R.drawable.ic_launcher); dialog6.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog6.setProgress(100); dialog6.setCancelable(false); dialog6.setButton("完成", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { isFlag = false; dialog6.dismiss(); } }); dialog6.show(); new Thread(){ public void run() { int number = 0; while (isFlag) { dialog6.setProgress(number); number++; if(number > 100){ isFlag = false; dialog6.cancel(); } try { Thread.sleep(80); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; }.start(); } }); this.findViewById(R.id.button7).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog7.show(); } }); this.findViewById(R.id.button8).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog8.show(); } }); } }