欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對話框的功能

 更新時(shí)間:2017年04月12日 09:19:56   作者:SoftXJ  
本篇文章主要介紹了Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對話框的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

這里的問題:當(dāng)我點(diǎn)擊確定按鈕,也就是 AlertDialog 里的 PositiveButton 的時(shí)候,我們需要判斷用戶是輸入是否符合我們的預(yù)期,如果不符合通常提示用戶重寫輸入,且不關(guān)閉當(dāng)前的對話框,但上圖中點(diǎn)擊按鈕后會自動的關(guān)閉窗口。

先看原來的這個(gè)是怎么寫的:

private void openDialog() {
 LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.change_password_dialog, null);
 final EditText originPasswordEt = (EditText) linearLayout.findViewById(R.id.origin_password);
 TextView forgetPassword = (TextView) linearLayout.findViewById(R.id.forget_password);

 final AlertDialog dialog = new AlertDialog.Builder(getContext())
  .setView(linearLayout)
  .setPositiveButton("確定", new DialogInterface.OnClickListener() {
   @Override
    public void onClick(DialogInterface dialog, int which) {
    String originPassword = originPasswordEt.getText().toString().trim();
     //傳到后臺
    }
  })
  .create();

 dialog.show();
}

雖然圖片里和代碼的并不是同一個(gè),但問題是一樣的

setPositiveButton 方法中,即使我們沒有調(diào)用 dialog.dismiss()

但對話框還是會自動的關(guān)閉,就算我們在 onClick 里判斷輸入的內(nèi)容,錯(cuò)誤的提示也會在窗口關(guān)閉后才出現(xiàn)。

在 AlertDialog 提供的 API 中我也沒有找到可以設(shè)置的地方,如果有還請告知。而我解決這個(gè)問題的辦法:

final AlertDialog dialog = new AlertDialog.Builder(getActivity())
  .setTitle(msg)
  .setView(layout)
  .setNegativeButton("取消", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
   }
  })
  .setPositiveButton("submit",null)
  .setCancelable(true)
  .create();
dialog.show();

//為了避免點(diǎn)擊 positive 按鈕后直接關(guān)閉 dialog,把點(diǎn)擊事件拿出來設(shè)置
dialog.getButton(AlertDialog.BUTTON_POSITIVE)
  .setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View v) {
  Pattern pattern = Pattern.compile("[0-9]*");
  Matcher matcher = pattern.matcher(editText.getText());
  if (!matcher.matches()){
   showToast("請輸入正確的 ID");
   break;
  }
  dialog.dismiss();
  }
 }
});

setPositiveButton("submit",null) 監(jiān)聽事件傳入 null

在調(diào)用 dialog.show() 后再設(shè)置 Button 的點(diǎn)擊事件,否則 getButton() 會返回空

這樣在我們手動調(diào)用 dialog.dismiss() 之前,對話框是不會關(guān)閉的。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論