Builder 类:实现对话框,提示框;
使用方法:实例化一个对象 ; Builder builder = new Builder(context); 构造方法里面的参数是:上下文菜单;

xml中的测试代码:
<!-- 主界面显示 --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <!-- 对话框测试按钮 -->    <Button         android:id="@+id/general_btn"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/generalbtnMessage"        />         <Button         android:id="@+id/edit_btn"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/editBtnMessage"        /></LinearLayout>xml:用于显示在对话框上的布局:<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <TableRow        android:layout_width="wrap_content"        android:layout_height="wrap_content" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="user name:" />        <EditText            android:layout_weight="0.5"            android:layout_width="fill_parent"            android:layout_height="wrap_content" />    </TableRow></TableLayout>


java代码:
package com.example.notebook_dialog;import android.app.Activity;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TableLayout;import android.widget.Toast;public class DialogActivity extends Activity {private Builder builder = null;private Button generalBtn,editBtn;protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);/*  测试普通对话框  *//* 首先通过new ,获得一个实体对象,构造方法接受的是一个Context  */builder = new Builder(this);/* 获取Button的实体对象,通过findViewById();方法 */generalBtn = (Button) findViewById(R.id.general_btn);editBtn = (Button) findViewById(R.id.edit_btn);/* 绑定事件 */generalBtn.setOnClickListener(general_listener);editBtn.setOnClickListener(edit_listener);}/* 普通对话框测试 *//* 编写按钮点击事件View.OnClickListener */OnClickListener general_listener = new OnClickListener() {public void onClick(View v) {/* 创建一个对话框,设置Title icon message button 等属性 *//* 设置Title ,标题  */builder.setTitle("普通对话框测试");/* 设置标题图标  */builder.setIcon(getResources().getDrawable(R.drawable.ic_launcher));/* 设置会话框中的内容  */builder.setMessage(getString(R.string.general_dialog_message));/* 设置会话框中的按钮 :确定setPositiveButton   *//* 注:不同的包中相同名称的类的功能是不一样的,必要时明确纽带关系 :DialogInterface.OnClickListener */builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {Toast.makeText(DialogActivity.this, "您选择了确定按钮", 1000).show();}});/* 设置会话框中的按钮:取消 setNegativeButton */builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {Toast.makeText(DialogActivity.this, "您选择了取消按钮", 1000);}});/* 对话框需要创建和显示 */builder.create().show();}};/* 编辑对话框测试  *//* 首先在layout布局文件中编写需要布局的样式   */OnClickListener edit_listener = new OnClickListener() {public void onClick(View v) {/* 创建一个对话框,设置Title icon message button 等属性 *//* 设置Title ,标题  */builder.setTitle("编辑对话框测试");/* 设置标题图标  */builder.setIcon(getResources().getDrawable(R.drawable.ic_launcher));/* 对话框中的编辑获取 *//* 将xml文件转换为view *//* 参1:布局文件ID; 参2:转换为View之后所指定的父类  */TableLayout tab = (TableLayout) getLayoutInflater().inflate(R.layout.dialolayout, null);/* 将绑定的布局显示在界面布局上  */builder.setView(tab);/* 设置会话框中的按钮 :确定setPositiveButton   *//* 注:不同的包中相同名称的类的功能是不一样的,必要时明确纽带关系 :DialogInterface.OnClickListener */builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {Toast.makeText(DialogActivity.this, "您选择了确定按钮", 1000).show();}});/* 设置会话框中的按钮:取消 setNegativeButton */builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {Toast.makeText(DialogActivity.this, "您选择了取消按钮", 1000);}});/* 创建和显示 */builder.create().show();}};}

更多相关文章

  1. java.io.IOException: Unable to open sync connec...
  2. Android判断定位功能是否可用
  3. android studio 安装PlantUML插件
  4. Android(安卓)Camera 明细
  5. android 采集PCM音频数据并播放(支持USB摄像头MIC)
  6. Android初级教程XUtils实现“断点续传”下载
  7. android CTS test
  8. Android(安卓)Dialog 应用
  9. Android微信之简单文本分享(ShareSDK-Eclipse)

随机推荐

  1. android 笔记:判断手机是否显示虚拟按键
  2. Android(安卓)APP 启动时间统计
  3. Android实现EditText正则表达式过滤
  4. android拼接多张bitmap图片
  5. Android判断是否有网络连接
  6. Android(安卓)Socket 编程
  7. Android(安卓)VTS学习
  8. Android(安卓)读取安装的非系统应用程序
  9. 我的android绘图学习笔记(二)
  10. android完全退出程序