Adroid 之 Builder 对话框
16lz
2021-01-24
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();}};}
更多相关文章
- java.io.IOException: Unable to open sync connec...
- Android判断定位功能是否可用
- android studio 安装PlantUML插件
- Android(安卓)Camera 明细
- android 采集PCM音频数据并播放(支持USB摄像头MIC)
- Android初级教程XUtils实现“断点续传”下载
- android CTS test
- Android(安卓)Dialog 应用
- Android微信之简单文本分享(ShareSDK-Eclipse)