亲宝软件园·资讯

展开

android 对话框 Android Dialog对话框实例代码讲解

xixi袭阳 人气:0

Dialog的基本方法

//创建Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//设置标题图标
builder.setIcon(R.drawable.ic_launcher);
//设置标题
builder.setTitle("这是一个对话框");
//设置信息
builder.setMessage("是否要跳转?");
//确定按钮
setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
//取消按钮
setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener)
//忽略
setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener)
//显示对话框
show();

系统样式

1.下拉列表

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
     builder.setIcon(R.drawable.ic_launcher);
     builder.setTitle("选择一个城市");
     //下拉列表的数据源
     final String[] cities = {"北京", "上海", "广州", "深圳", "杭州"};
     builder.setItems(cities, new DialogInterface.OnClickListener(){
       @Override
       public void onClick(DialogInterface dialog, int which){
         Toast.makeText(MainActivity.this, "你选择的城市为:" + cities[which], Toast.LENGTH_SHORT).show();
       }
 });
 builder.show();

2.单选框列表

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setIcon(R.drawable.ic_launcher);
    builder.setTitle("请选择性别");
    final String[] sex = {"男", "女"};
    //第二个参数指定默认哪个单选框被勾中
    builder.setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener(){
      @Override
      public void onClick(DialogInterface dialog, int which){
          Toast.makeText(MainActivity.this, "性别为:" + sex[which], Toast.LENGTH_SHORT).show();
      }
    });
    builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
      @Override
      public void onClick(DialogInterface dialog, int which){
            
      }
    });
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
      @Override
      public void onClick(DialogInterface dialog, int which){
            
      }
    });
builder.show();

3.多选框列表

String str;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("选择你想看的电视剧");
final String[] hobbies = {"廷禧攻略", "扶摇", "香蜜沉沉烬如霜", "如懿传"};
//第二个参数代表哪几个选项被选择,需要传递一个boolean[]数组进去,其长度要和第一个参数的长度相同,如果null表示都不选
builder.setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener(){
  @Override
  public void onClick(DialogInterface dialog, int which, boolean isChecked){
    if(isChecked){
      str.append(hobbies[which] + ", ");
    }
    Toast.makeText(MainActivity.this, "选择的是:" + str.toString(), Toast.LENGTH_SHORT).show();
  }
 });
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
  @Override
  public void onClick(DialogInterface dialog, int which){

  }
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
  @Override
  public void onClick(DialogInterface dialog, int which){

  }
});
builder.show();

 4.等待对话框

ProgressDialog waitingDialog= new ProgressDialog(MainActivity.this);
waitingDialog.setTitle("等待加载中,请稍后...");
waitingDialog.setMessage("等待中...");
waitingDialog.setIndeterminate(true);//采用不确定式的进度条
waitingDialog.setCancelable(false);//点击外部不取消对话框
waitingDialog.show();

5.进度条对话框

int MAXPD = 100;
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgress(0);//设置默认值
progressDialog.setTitle("正在下载");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//进度条样式
progressDialog.setMax(MAXPD);//设置最大值
progressDialog.show();

自定义Dialog

1.继承Dialog

public class CustomDialog extends Dialog {
//标题
protected TextView hintTv;
//左边按钮
protected Button doubleLeftBtn;
//右边按钮
protected Button doubleRightBtn;
//输入框
public EditText editText;
public CustomDialog(Context context) {
  super(context, R.style.CustomDialogStyle);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.setCancelable(false); // 是否可以撤销
  setContentView(R.layout.dialog_custom);
  hintTv = findViewById(R.id.tv_common_dialog_hint);
  doubleLeftBtn = findViewById(R.id.btn_common_dialog_double_left);
  doubleRightBtn = findViewById(R.id.btn_common_dialog_double_right);
  editText = findViewById(R.id.et_common_dialog);
}
//置右键文字和点击事件
public void setRightButton(String rightStr, View.OnClickListener clickListener) {
  doubleRightBtn.setOnClickListener(clickListener);
  doubleRightBtn.setText(rightStr);
}
//设置左键文字和点击事件
public void setLeftButton(String leftStr, View.OnClickListener clickListener) {
  doubleLeftBtn.setOnClickListener(clickListener);
  doubleLeftBtn.setText(leftStr);
}
//设置提示内容
public void setHintText(String str) {
  hintTv.setText(str);
  hintTv.setVisibility(View.VISIBLE);
}
//给两个按钮 设置文字
public void setBtnText(String leftStr, String rightStr) {
  doubleLeftBtn.setText(leftStr);
  doubleRightBtn.setText(rightStr);
}
}

2.自定义Style

<style name="CustomDialogStyle" parent="@android:style/Theme.Dialog">
    <!-- 边框 -->
    <item name="android:windowFrame">@null</item>
    <!-- 背景透明 -->
    <item name="android:windowBackground">@color/transparent</item>
    <!-- 无标题 -->
    <item name="android:windowNoTitle">true</item>
    <!-- 是否浮现在activity之上 -->
    <item name="android:windowIsFloating">true</item>
    <!-- 半透明 -->
    <item name="android:windowIsTranslucent">false</item>
    <!-- 背景模糊 -->
    <item name="android:windowContentOverlay">@null</item>
    <!-- 允许对话框的背景变暗 -->
    <item name="android:backgroundDimEnabled">true</item>
    <!-- 字体颜色 -->
    <item name="android:textColor">@color/white</item>
    <item name="android:editTextColor">@color/white</item>
 </style>

3.自定义布局

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/ll_common_dialog_layout"
  android:layout_width="500dp"
  android:layout_height="250dp"
  android:layout_margin="5dp"
  android:background="@drawable/background_info"
  android:orientation="vertical"
  android:gravity="center">

  <!--提示信息-->
  <TextView
    android:id="@+id/tv_common_dialog_hint"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="@color/white"
    android:textSize="27sp"/>
  <EditText
    android:id="@+id/et_common_dialog"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:textColor="@color/back"
    android:inputType="text|textNoSuggestions"
    tools:ignore="LabelFor"
    android:hint="请输入密码"/>
  
  <!--底部按钮-->
  <LinearLayout
    android:id="@+id/ll_common_dialog_double"
    android:layout_width="360dp"
    android:layout_height="60dp"
    android:layout_margin="20dp"
    android:orientation="horizontal">

    <Button
      android:id="@+id/btn_common_dialog_double_left"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/btnselector"
      android:gravity="center"
      android:textColor="@color/white"
      android:textSize="27dp"/>

    <Button
      android:id="@+id/btn_common_dialog_double_right"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/btnselector"
      android:gravity="center"
      android:textColor="@color/white"
      android:textSize="27dp"/>
  </LinearLayout>
</LinearLayout>

4.ipad隐藏底部虚拟按键

//弹出dialog时隐藏底部虚拟按键
dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
dialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
  @Override
  public void onSystemUiVisibilityChange(int visibility) {
    int uiOptions =View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    if (Build.VERSION.SDK_INT >= 19) {
      uiOptions |= 0x00001000;
    } else {
      uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
    }
    dialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
  }
});

5.使用自定义Dialog

CustomDialog dialog = new CustomDialog(this);
dialog.setHintText("请输入密码");
dialog.setLeftButton("取消", new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      dialog.dismiss();
    }
});
dialog.setRightButton("确定", new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        
    }
});
dialog.show();

总结

以上所述是小编给大家介绍的Android Dialog对话框实例代码讲解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

加载全部内容

相关教程
猜你喜欢
用户评论