温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Dialog中Android应用中的使用方法

发布时间:2020-12-04 16:24:58 来源:亿速云 阅读:183 作者:Leah 栏目:移动开发

Dialog中Android应用中的使用方法?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

Android开发中最常用的就是Dialog类,除了自定义dialog布局,最多的就是用在弹出对话框、进度条、输入框、单选、复选框。

1、选择对话框:

AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
dialog.setTitle("选择对话框"); 
dialog.setMessage("请选择确认或取消"); 
dialog.setCancelable(false); //设置按下返回键不能消失 
dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() { 
  @Override 
  public void onClick(DialogInterface dialog, int which) { 
    Toast.makeText(MainActivity.this, "您选择了确认", Toast.LENGTH_LONG).show(); 
      } 
    }); 
    dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
        Toast.makeText(MainActivity.this, "您选择了取消", Toast.LENGTH_LONG).show(); 
      } 
    }); 
dialog.show();//显示弹出窗口 

2、进度条

ProgressDialog progressDialog = new ProgressDialog(this); 
progressDialog.setTitle("显示进度条"); 
progressDialog.setMessage("加载中..."); 
progressDialog.setCancelable(true); 
progressDialog.show(); 

3、弹出输入框

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("弹出输入框"); 
builder.setMessage("输入内容"); 
final EditText editText = new EditText(this); 
builder.setView(editText); 
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 
      @Override 
  public void onClick(DialogInterface dialog, int which) { 
    Toast.makeText(MainActivity.this, "您输入了" + editText.getText().toString(), Toast.LENGTH_SHORT).show(); 
      } 
  }); 
builder.setNegativeButton("取消", null); 
builder.show(); 

4、弹出单选框

final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("弹出单选框"); 
final String[] choice = new String[]{"Java", "C++", "PHP"}; 
builder.setSingleChoiceItems(choice, 0, 
  new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
      Toast.makeText(MainActivity.this, "您选择了" + choice[which], Toast.LENGTH_LONG).show(); 
      dialog.dismiss(); // 让窗口消失 
    } 
  }); 
builder.create().show(); 

5、弹出复选框

final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("弹出多选框"); 
final String[] choice = new String[]{"Android", "IOS", "Windos Phone"}; // 选择项 
final boolean[] isCheck = new boolean[choice.length]; // 是否被选中 
builder.setMultiChoiceItems(choice, null, 
  new DialogInterface.OnMultiChoiceClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
      if (isChecked == true) 
        isCheck[which] = true; 
      else if(isChecked == false) 
        isCheck[which] = false; 
     } 
  }); 
builder.setNegativeButton("取消", null); 
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 
  @Override 
  public void onClick(DialogInterface dialog, int which) { 
    String isSelected = "您选择了"; 
    for (int i = 0; i < choice.length; i++) { 
      if (isCheck[i] == true) { 
        isSelected += choice[i]+","; // 将选择的内容加上 
      } 
    } 
    Toast.makeText(MainActivity.this, isSelected, Toast.LENGTH_LONG).show(); 
   } 
}); 
builder.create(); 
builder.show(); 


6、自定义布局的Dialog

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
 final AlertDialog dialog = builder.create(); 
 View view = View.inflate(this, R.layout.personal_modifyhead_dialog, null); 
 dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题 
 dialog.show(); 
 
 AutoRelativeLayout rl_photograph = (AutoRelativeLayout) view.findViewById(R.id.rl_photograph); 
 rl_photograph.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View v) { 
     presenter.modiftHeadFromPhotograph(); 
     dialog.dismiss(); 
   } 
 }); 
 
 AutoRelativeLayout rl_album = (AutoRelativeLayout) view.findViewById(R.id.rl_album); 
 rl_album.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View v) { 
     presenter.modifyHeadFromAlbum(); 
     dialog.dismiss(); 
   } 
 }); 

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI