Android经常使用的五种弹出对话框[通俗易懂]

Android经常使用的五种弹出对话框

大家好,又见面了,我是全栈君。

  一个Android开发中经常使用对话框的小样例,共同拥有五种对话框:普通弹出对话框,单选对话框,多选对话框,输入对话框及进度条样式对话框:

<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” >

    <Button
        android:id=”@+id/common_dialog”
        android:layout_width=”match_parent”
        android:layout_height=”40dp”
        android:text=”普通对话框”
        android:textSize=”16sp”
        android:layout_marginTop=”10dp” />

    <Button
        android:id=”@+id/radio_dialog”
        android:layout_width=”match_parent”
        android:layout_height=”40dp”
        android:text=”单选对话框”
        android:textSize=”16sp”
        android:layout_marginTop=”10dp”  />

    <Button
        android:id=”@+id/check_dialog”
        android:layout_width=”match_parent”
        android:layout_height=”40dp”
        android:text=”多选对话框” 
        android:textSize=”16sp”
        android:layout_marginTop=”10dp” />

    <Button
        android:id=”@+id/input_dialog”
        android:layout_width=”match_parent”
        android:layout_height=”40dp”
        android:text=”输入文字对话框” 
        android:textSize=”16sp”
        android:layout_marginTop=”10dp” />

    <Button
        android:id=”@+id/progress_dialog”
        android:layout_width=”match_parent”
        android:layout_height=”40dp”
        android:text=”进度条对话框” 
        android:textSize=”16sp”
        android:layout_marginTop=”10dp” />

</LinearLayout>

以下是输入内容的简单布局activity_input.xml

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    xmlns:tools=”http://schemas.android.com/tools”
    android:id=”@+id/LinearLayout1″
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”vertical” >

    <TextView
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:text=”@string/hello_world” />

    <EditText
        android:id=”@+id/uname”
        android:layout_width=”fill_parent”
        android:layout_height=”wrap_content” />

    <TextView
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:text=”@string/hello_world” />

    <EditText
        android:id=”@+id/upass”
        android:layout_width=”fill_parent”
        android:layout_height=”wrap_content” />

</LinearLayout>

代码及凝视:

public class MainActivity extends Activity implements OnClickListener {
/**单选框模拟标题 大学*/
private final static int CHECKED_ENU = 0;
/**单选框模拟标题  高中*/
private final static int CHECKED_SEL = 1;
/**单选框模拟标题  初中*/
private final static int CHECKED_CHU = 2;
/**复选button状态为全选 */
private boolean[] checked = { true, true, true, false };
/**模拟的进度值 */
private int progressNumber;
/**进度对话框 */
private ProgressDialog progressDialog;
/**相应button*/
private Button commonBtn, radioBtn, checkBtn, inputBtn, progressBtn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initListeners();
}

/**初始化UI控件*/

private void initViews() {
this.commonBtn = (Button) findViewById(R.id.common_dialog);
this.radioBtn = (Button) findViewById(R.id.radio_dialog);
this.checkBtn = (Button) findViewById(R.id.check_dialog);
this.inputBtn = (Button) findViewById(R.id.input_dialog);
this.progressBtn = (Button) findViewById(R.id.progress_dialog);
}

/**注冊button监听事件*/
private void initListeners() {
this.commonBtn.setOnClickListener(this);
this.radioBtn.setOnClickListener(this);
this.checkBtn.setOnClickListener(this);
this.inputBtn.setOnClickListener(this);
this.progressBtn.setOnClickListener(this);
}

/**普通对话框 */
private Dialog buildAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(“对话框”);
builder.setMessage(“您的password不正确!!”);

ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.mm1);
/**设置背景图片*/
builder.setView(imageView);
/**左边button*/
builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击的是左边确定button!”);
}
});
/**中间button*/
builder.setNeutralButton(“详情”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击的是中间详情button!”);
}
});
/**右边button*/
builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
setTitle(“您点击的是右边取消button!”);
}
});
return builder.create();
}

/**单选button弹出框 */
private Dialog buildAlertDialog_radio() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(“对话框”);
/**单选button,默认高中被选中*/
builder.setSingleChoiceItems(new String[] { “大学”, “高中”, “初中”, “小学” }, 1, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch (which) {
case CHECKED_ENU:
setTitle(“大学”);
break;
case CHECKED_SEL:
setTitle(“高中”);
break;
case CHECKED_CHU:
setTitle(“初中”);
break;
default:
setTitle(“小学”);
break;
}
}
});

builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击的是左边确定button!”);
}
});
builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击的是右边取消button!”);
}
});
return builder.create();
}

/**能够多选button弹出框 */
private Dialog buildAlertDialog_checkbox() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(“对话框”);
/**复选button*/
builder.setMultiChoiceItems(new String[] { “大学”, “高中”, “初中”, “小学” }, checked, new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
setTitle(“which=” + which + “—–” + “isChecked=” + isChecked);
}
});

builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击了确定button!”);
}
});
builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
setTitle(“您点击的是了取消button!”);
}
});
return builder.create();
}

/**含能够输入文本的弹出框 */
private Dialog buildAlertDialog_input() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(“对话框”);
LayoutInflater inflater = LayoutInflater.from(this);
builder.setView(inflater.inflate(R.layout.activity_input, null));
builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击的是确定button!”);
}
});
builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle(“您点击的是取消button!”);
}
});
return builder.create();
}

/**进度对话框 */
private Dialog buildAlertDialog_progress() {
progressDialog = new ProgressDialog(this);
progressDialog.setTitle(“进度条”);
progressDialog.setMessage(“正在下载………..”);
/**进度条样式 */
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
/**模糊效果 */
progressDialog.setIndeterminate(false);
return progressDialog;
}

/**每隔0.3秒更新一次进度 */
public void updateProgress() {
new Thread() {
@Override
public void run() {
try {
while (progressNumber <= 100) {
progressDialog.setProgress(progressNumber++);
Thread.sleep(300);
super.run();
}
/**下载完后,关闭下载框 */
progressDialog.cancel();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.common_dialog:
buildAlertDialog().show();
break;
case R.id.radio_dialog:
buildAlertDialog_radio().show();
break;
case R.id.check_dialog:
buildAlertDialog_checkbox().show();
break;
case R.id.input_dialog:
buildAlertDialog_input().show();
break;
case R.id.progress_dialog:
buildAlertDialog_progress().show();
updateProgress();
break;
default:
break;
}
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115694.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 不是单组分组函数「建议收藏」

    不是单组分组函数「建议收藏」问题:一:SELECT tablespace_name, SUM(bytes) freeFROM dba_free_space不是单组分组函数原因: 1、如果程序中使用了分组函数,则有两种情况可以使用:程序中存在group by,并指定了分组条件,这样可以将分组条件一起查询出来改为:  SELECT tablespace_name, SUM(bytes) freeFROM dba_free_spa…

    2022年6月30日
    25
  • Double转BigDecimal类型互转,保留俩位小数。

    Double转BigDecimal类型互转,保留俩位小数。Double转BigDecimalDoublechannelPrice=3.1452;BigDecimala=newBigDecimal(channelPrice);BigDecimalb=a.setScale(2,RoundingMode.HALF_UP);System.out.println(b);//b=3.14

    2022年5月30日
    125
  • MQTT服务器搭建 windows[通俗易懂]

    MQTT服务器搭建 windows[通俗易懂]typora-copy-images-to:upload软件简介MQ遥测传输(MQTT)是轻量级基于代理的发布/订阅的消息传输协议,设计思想是开放、简单、轻量、易于实现。这些特点使它适用于受限环境。例如,但不仅限于此:网络代价昂贵,带宽低、不可靠。在嵌入设备中运行,处理器和内存资源有限。该协议的特点有:使用发布/订阅消息模式,提供一对多的消息发布,解除应用程序耦合。对负载内容屏蔽的消息传输。使用TCP/IP提供网络连接。有三种消息发布服务质量:“至多一次”,消息发布.

    2022年4月29日
    49
  • 去重和简单遍历

    去重和简单遍历去重和简单遍历

    2022年4月24日
    48
  • 非满秩矩阵也能求逆矩阵吗_广义逆矩阵的性质

    非满秩矩阵也能求逆矩阵吗_广义逆矩阵的性质今天遇到一个很奇怪的问题:一个方阵,逆矩阵存在,但不是满秩。问题来源  在实际应用的时候,发现返回值都是0,于是跟踪到这里,发现了这个问题:JtJ不是满秩,因此JtJN保持初始化的零值。matJtJN=zeros(N,N);matResult=zeros(N,1);if(N==rank(JtJ)){JtJN=inv(JtJ);}for…

    2022年10月25日
    0
  • python 正则表达式匹配数字或者小数点_五位小数正则表达式

    python 正则表达式匹配数字或者小数点_五位小数正则表达式在对文本关键信息进行提取的过程中,通常需要使用正则表达式匹配。这篇笔记整理汇总Python中可能用到的与数值相关的正则表达式。正则表达式基础正则表达式是用字符串表示的一种语法,用于描述一种字符串匹配的模式。正则表达式中大多数字符的含义是通用的,比如符号^和$在绝大多数语言的正则表达式中都表示行头和行尾;但也可能在某些语法上存在差异,这需要依据特定语言而定。Python的正则表达式匹…

    2022年10月14日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号