1

上下文:有一個自定義Listview,每個列表項都有一個按鈕。當您單擊按鈕時,會出現一個帶有編輯文本和提交按鈕的alertDialog。這隻會在第一次點擊時發生,隨後的點擊Toast將僅以其點擊次數顯示。Android-幫助修復帶有EditText的自定義警報對話框

當您點擊提交按鈕時,會出現一個烤麪包,顯示在editText中輸入的文本以及點擊它的次數,推測總是爲1,因爲這隻能在第一次點擊時發生。

問題:如果用戶在點擊提交之前點擊editText,那麼timesClicked計數器無法正常工作。我猜測它正在休息。但是,如果用戶沒有而不是,請點擊editText,然後程序正常工作。 0_o我不知所措。

嘗試解決:我將代碼簡化了很多,試圖找出問題並找出問題所在。最初我膨脹了一個只有編輯文本的視圖,然後我只是使用builder.setPositiveButtton。我認爲直接在視圖中實現按鈕可以修復它,但似乎並非如此。我一直堅持這一點。任何幫助將是巨大的

Here is a video of the bug happening

private class OnSubtractClickListener implements View.OnClickListener { 

    final int id; //id of list item that was clicked 
    int timesClicked; 
    Toast toast; 

    public OnSubtractClickListener(int id, View view) { 
     super(); 
     this.id = id; 
     timesClicked = 0; 
    } 

@Override 
    public void onClick(View view) { 
     if (timesClicked != 0) { 
      toast.setText(Integer.toString(timesClicked)); 
      toast.show(); 
     } 
     else{ 
      toast = Toast.makeText(view.getContext(), "", Toast.LENGTH_SHORT); 
      final View dialogView = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_add_notes, null); 

      AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); 
      builder.setView(dialogView); 
      builder.setTitle("Subtract cigar?"); 
      builder.setIcon(R.mipmap.monkey_launcher); 
      final AlertDialog dialog = builder.create(); 

      Button yesButton = (Button)dialogView.findViewById(R.id.dialog_notes_yes_button); 
      yesButton.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        EditText editText = (EditText)dialogView.findViewById(R.id.dialog_editText); 
        String userInput = editText.getText().toString(); 
        String timesClickedString = Integer.toString(++timesClicked); 
        toast.setText(timesClickedString + ": " + userInput); 
        toast.show(); 
        dialog.dismiss(); 
       } 
      }); 

      dialog.show(); //new 
     } 
    } 
} 
+1

爲了讓次的點擊回到0,則必須在某處重新創建點擊監聽器。你可以顯示你在代碼中設置點擊監聽器的位置嗎? – AdamMc331

+0

它只在用戶點擊對話框中的editText時才被重置。否則,它工作正常。我設置它在一個bindView的customlistAdapter – Dillon

+0

沒有人知道這個問題?在我的應用程序中,數據庫正在被調用,並且正在更新列表視圖。但是,然後listview實際上恢復到以前的狀態。但數據庫已更新。沒有意義。有什麼可以取消對列表視圖項的更改 – Dillon

回答

0

您可以擴展對話框類。

例如:

public class CustomDialog extends Dialog { 
    private EditText editText;   

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);   

    WindowManager.LayoutParams lpWindow = new WindowManager.LayoutParams(); 
    lpWindow.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND; 
    lpWindow.dimAmount = 0.8f; 
    getWindow().setAttributes(lpWindow); 

    setContentView(R.layout.activity_custom_dialog); 
    editText = (EditText) findViewById(R.id.editText);   
    } 
} 

您可以使用此對話框..

mCustomDialog = new CustomDialog(); 
mCustomDialog.show(); 

您可以進行佈局,如你所願。

============================================== ========================

您可以使用AlertDialog.Builder.setPositiveButton。

網站:setPositiveButton

例如...

toast = Toast.makeText(view.getContext(), "", Toast.LENGTH_SHORT); 
final View dialogView = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_add_notes, null); 
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); 
     builder.setView(dialogView); 
     builder.setTitle("Subtract cigar?"); 
     builder.setIcon(R.mipmap.monkey_launcher); 
     builder.setPositiveButton("text", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialogInterface, int which) { 
       EditText editText = (EditText)dialogView.findViewById(R.id.dialog_editText); 
       String userInput = editText.getText().toString(); 
       String timesClickedString = Integer.toString(++timesClicked); 
       toast.setText(timesClickedString + ": " + userInput); 
       toast.show(); 
      } 
     }); 
     final AlertDialog dialog = builder.create(); 
     dialog.show(); //new 
+0

感謝您的嘗試,但這不是我所期待的。我沒有在對話框內創建editText的問題。這部分很容易。我的問題是,當用戶點擊editText並出現虛擬鍵盤時。對話框關閉後,listView會做什麼改變。重置。 – Dillon

+0

再次感謝您的嘗試,但問題不在於使用editText創建對話框。我發現問題所在,並提出了一個解決方案,我將在今晚晚些時候發佈。基本上發生的事情是,當鍵盤出現時,它會導致列表視圖調整重新創建整個列表視圖的大小與對話框出現之前列表項目的reycycled /舊版本 - 有效地撤消對話框對ListView項目所做的任何更改。 – Dillon

0

我找到了解決辦法。基本上發生的事情是,當鍵盤出現時,它會導致列表視圖調整大小重新創建整個列表視圖與回收/舊版本的列表項目從對話框出現之前 - 有效地撤消對對話框對ListView項目所做的任何更改。

在你的ListView XML補充一點:

機器人:descendantFocusability = 「beforeDescendants」

在Mainfest.xml:

<activity android:name= ".yourActivity"  
android:windowSoftInputMode="adjustPan"/>