【安卓笔记】盘点android中的dialog
public void show_alert() { AlertDialog.Builder builder = new Builder(this); builder.setMessage("是否更新"); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("提醒"); builder.setPositiveButton("确定",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"点击了确定按钮",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"点击了取消按钮",Toast.LENGTH_SHORT).show(); // dialog.dismiss(); } }); builder.show(); }
private void show_list() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("职业"); builder.setItems(new String[]{"程序员","工程师","砖家"},new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"点击了"+which,Toast.LENGTH_SHORT).show(); } }); builder.show(); }
public void show_dialog() { /* 这里的数据可以从数据库/网络中动态获取 */ List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("image", R.drawable.image1); map1.put("text", "我是条目1"); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("image", R.drawable.image2); map2.put("text", "我是条目2"); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("image", R.drawable.image3); map3.put("text", "我是条目3"); data.add(map1); data.add(map2); data.add(map3); SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_item, new String[] { "image", "text" }, new int[] { R.id.iv_show, R.id.tv_text }); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("从适配器中获取的数据"); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "点击了" + which, 0).show(); } }); builder.show(); }
代码片段:
public void show_single() { AlertDialog.Builder builder = new Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("提醒"); builder.setSingleChoiceItems(new String[]{"条目1","条目2","条目3"},0,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which)//id从0开始 { Toast.makeText(MainActivity.this,"item="+which,Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); builder.setNegativeButton("取消",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"点击了取消按钮",Toast.LENGTH_SHORT).show(); } }); builder.show(); }
代码片段:
public void show_multiple() { AlertDialog.Builder builder = new Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("提醒"); builder.setMultiChoiceItems(new String[]{"条目1","条目2","条目3","条目4"}, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(MainActivity.this,"item"+which+","+isChecked,Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"点击了取消按钮",Toast.LENGTH_SHORT).show(); } }); builder.show(); }
private void show_own_dialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = this.getLayoutInflater();//"充气筒" builder.setView(inflater.inflate(R.layout.dialog, null)) .setPositiveButton("登陆", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { Toast.makeText(MainActivity.this, "确定", 0).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Toast.makeText(MainActivity.this,"取消", 0).show(); } }); builder.show(); }
public void show_circle_progressdialog() { ProgressDialog diaolog = new ProgressDialog(this); diaolog.setIcon(R.drawable.ic_launcher); diaolog.setTitle("提醒"); diaolog.setMessage("正在加载"); diaolog.show(); }
public void show_horizonal() { final ProgressDialog diaolog = new ProgressDialog(this); diaolog.setIcon(R.drawable.ic_launcher); diaolog.setTitle("提醒"); diaolog.setMessage("正在加载"); diaolog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); diaolog.setMax(100); new Thread(new Runnable() { @Override public void run() { for(int i = 0; i<=100; i++) { diaolog.setProgress(i); try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } } diaolog.dismiss(); } }).start(); diaolog.show(); }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <DatePicker android:id="@+id/dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/tp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>activity:
package com.example.dialogdemo4; import android.app.Activity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.Toast; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class MainActivity extends Activity { private DatePicker dp = null; private TimePicker tp = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dp = (DatePicker) findViewById(R.id.dp); tp = (TimePicker) findViewById(R.id.tp); dp.init(2014, 1, 1, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(MainActivity.this,"monthOfYear"+monthOfYear+",dayOfMonth"+dayOfMonth, 0).show(); } }); tp.setIs24HourView(true); tp.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { Toast.makeText(MainActivity.this,"hourOfDay"+hourOfDay+",minute"+minute, 0).show(); } }); } }
--------------------------------------------------------完--------------------------------------------------------
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。