2013-05-01 69 views
1

我在android中創建了一個包含EditText和ListView的自定義對話框。當我選擇任何Item時,列表視圖onItemClickListener會正確觸發,但EditText的偵聽器並非如此。EditText監聽器在對話框中未被觸發

這是我的代碼:

EditText filterEditText; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

LayoutInflater factory = LayoutInflater.from(this); 
View content = factory.inflate(R.layout.dialog_layout, null); 
filterEditText = (EditText) content 
     .findViewById(R.id.filterEditText); 
filterEditText.addTextChangedListener(txtListener); 

............ 


    TextWatcher txtListener = new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 

      filterEditText.setText("text entered"); 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 
     } 
    }; 

回答

4

我正要刪除帖子,但我決定把在任何情況下,面臨着同樣的問題的解決方案。我碰到了同樣的問題,但在`DialogFragment`

public void createLocationsDialog() { 

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 

    builder.setTitle("Choose a location"); 

    LayoutInflater factory = LayoutInflater.from(MainActivity.this); 
    View content = factory.inflate(R.layout.dialog_layout, null); 

    ListView locationsList = (ListView) content 
      .findViewById(R.id.locationsListView); 
    filterEditText = (EditText) content 
      .findViewById(R.id.filterEditText); 

    ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(
      MainActivity.this, android.R.layout.simple_list_item_1, 
      data.getName()); 
    locationsList.setAdapter(modeAdapter); 

    builder.setView(content); 

    locationsDialog = builder.create(); 

    locationsList.setOnItemClickListener(listItemClicked); 
    filterEditText.addTextChangedListener(txtListener); 

    locationsDialog.show(); 
} 
+0

我通過移動從我的onCreate代碼到我在其中創建對話框的方法解決它。爲了更清楚地說明,在調用'builder.create()'之後添加'TextChangedListener'。 – Ivan 2017-06-02 17:45:20