AndroidStudio对话框总结

AndroidStudio对话框总结
在Android应⽤开发中,程序与⽤户交互的⽅式会直接影响到⽤户的使⽤体验,对话框⼜是与⽤户交互必不可少的部分。我们经常会需要在界⾯上弹出⼀个对话框,让⽤户点击对话框的某个按钮、选项,或者是输⼊⼀些⽂本,从⽽知道⽤户的做了什么操作,或是下达了什么指令。
对话框⼜可以分为⼏类:
1、普通对话框
2、列表对话框
3、单选对话框
4、多选对话框
5、等待对话框
6、进度条对话框
7、⾃定义对话框
废话就不多说啦,直接上代码,代码⾥都有详细的注释,如果不懂的话可以留⾔或者问⼀下度娘
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"    xmlns:app="schemas.android/apk/res-auto"
xmlns:tools="schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_1"
android:text="普通对话框1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myclick"/>
<Button
大聚合
android:id="@+id/btn_2"
android:text="普通对话框2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myclick"/>
<Button
android:id="@+id/btn_3"
android:text="列表对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myclick"/>
<Button
android:id="@+id/btn_4"
5-氯-2-戊酮android:text="单选对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myclick"/>
<Button
android:id="@+id/btn_5"
android:text="多选对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myclick"/>
<Button
android:id="@+id/btn_6"
android:text="等待对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myclick"/>
<Button
android:id="@+id/btn_7"
android:text="进度条对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myclick"/>
</LinearLayout>
MainActivity.java
ample.dialog;
import android.app.ProgressDialog;
t.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView (R.layout.activity_main);
}
public void myclick(View v){
switch (v.getId ()){
case R.id.btn_1:
showNormalDialog1();
break;
case  R.id.btn_2:
showNormalDialog2();
break;
case R.id.btn_3:
showListDialog();
break;
case R.id.btn_4:
showSingleDialog();
break;
case R.id.btn_5:
showMultiDialog();
break;
case R.id.btn_6:
showWaitDialog();
break;
case R.id.btn_7:
showProgressDialog();
break;
}
}
//AlertDialog的构造⽅法为protected,因此外包是⽆法使⽤的,所以我们要借助构建器Builder
private void showNormalDialog1() {
AlertDialog.Builder dialog = new AlertDialog.Builder (this);
dialog.setTitle ("温馨提⽰").setMessage ("你确定要退出此程序嘛?");
//点击确定就退出程序
dialog.setPositiveButton ("确定", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish ();
}
});
//如果取消,就什么都不做,关闭对话框
dialog.setNegativeButton ("取消",null);
dialog.show ();
}
private void showNormalDialog2(){
AlertDialog dialog2 = new AlertDialog.Builder (this).create ();
dialog2.setTitle ("坪价");
dialog2.setMessage ("请为本次的服务打分!");
dialog2.setButton (DialogInterface.BUTTON_POSITIVE, "5分", new DialogInterface.OnClickListener () {            @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText (MainActivity.this,"5分",Toast.LENGTH_LONG).show ();
}
});
dialog2.setButton (DialogInterface.BUTTON_NEGATIVE, "3分", new DialogInterface.OnClickListener () {            @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText (MainActivity.this,"3分",Toast.LENGTH_LONG).show ();
}
});
//⼀定要调⽤show()⽅法,否则对话框不会显⽰
dialog2.show ();
}
private void showListDialog(){
final String[] items = {"⼩王","⼩李" ,"⼩⽩"};
AlertDialog.Builder dialog3 = new AlertDialog.Builder (this).setTitle ("你是谁?")
.setItems (items, new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
/
/如果点击⼩王的话那么which保存的就是0
Toast.makeText (MainActivity.this,"我是:"+items[which],Toast.LENGTH_LONG).show ();
}
});
dialog3.show ();
}
int ide = 0;  //全局变量
private void showSingleDialog() {
final String[] stars = {"Jay","JJ" ,"Eson"};
final AlertDialog.Builder dialog4 = new AlertDialog.Builder (this)
.setTitle ("选择你喜欢的明星:")
/
/参数1:选项。参数2:默认选项。参数3:选中时的事件
.setSingleChoiceItems (stars, 0, new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText (MainActivity.this,"你选择了:"+stars[which],Toast.LENGTH_LONG).show ();                        ide = which;
}
}).setPositiveButton ("确定", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText (MainActivity.this,"你选择了:"+stars[ide],Toast.LENGTH_LONG).show ();
}
});  //点击确定后对话框消失,并且打印所选内容
dialog4.show ();
}
private void showMultiDialog() {
AlertDialog.Builder dialog5 = new AlertDialog.Builder (this);
final String[] movie = {"复联1","复联2","复联3","复联4"};
final boolean[] check = {true,false,true,false};
dialog5.setTitle ("你想看什么电影?")
//第⼀个参数是选项,第⼆个参数是默认备选项(true的是选中的),第三个参数是点击时触发的效果                .setMultiChoiceItems (movie, check, new DialogInterface.OnMultiChoiceClickListener () {
@Override
//第⼀个参数对话框本⾝,第⼆个参数是按钮的索引,第三个是标志按钮是否处于选中状态true
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Log.e("Log",movie[which]);
}
}).setPositiveButton ("确定", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
String msg = "你喜欢的电影是:";
for (int n=0;n<check.length;n++){
if(check[n]){
msg = msg + movie[n] + " ";
压缩空气汽车
Toast.makeText (MainActivity.this,msg,Toast.LENGTH_LONG).show ();
}
}
}
}
});
dialog5.show ();
}
private void showWaitDialog() {
//进度条对话框,默认是转圈
ProgressDialog progressDialog = new ProgressDialog (this);
progressDialog.setTitle ("下载中....");      //设置标题
progressDialog.setMessage ("请等待");
progressDialog.show ();
}
private void showProgressDialog() {
final ProgressDialog dialog7 = new ProgressDialog (this);
dialog7.setTitle ("下载中....");      //设置标题
dialog7.setMessage ("请等待");
dialog7.setProgressStyle (ProgressDialog.STYLE_HORIZONTAL);  // 设置进度条的风格(⽔平)        dialog7.setIndeterminate (false);  //设置进度条模糊(默认为True)
dialog7.show ();
//设置进度条的动态效果,需要创建⼀个线程
final Thread thread = new Thread () {
public void run(){
射线灯super.run ();
mjpg
//让进度条从1⾛到100
for (int i = 0;i<=100;i++){
dialog7.setProgress (i);
try {          //抛出异常
Thread.sleep (50);          //每⾛动⼀下休眠50毫秒
} catch (InterruptedException e) {
e.printStackTrace ();
}
}
dialog7.dismiss ();  //当进度条到达100后,对话框消失
}
};
验光组合
thread.start ();      //启动线程
}
}

本文发布于:2024-09-24 04:16:27,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/277396.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:对话框   进度条   参数   点击
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议