2016-06-11 65 views
0

我正在嘗試爲我的應用程序的設置創建自定義對話框,問題是自定義按鈕,編輯文本框不響應用戶活動。我希望有人能幫幫忙。 這裏是我的代碼:自定義對話框中的按鈕將不會響應

public class SettingsDialog extends DialogFragment { 
private EditText editText; 
private Spinner ipSpinner; 
private MainActivity mainActivity; 
private Settings settings; 
private int selectedIP; 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 
    final View dialogView = inflater.inflate(R.layout.dialog_settings, null); 
    mainActivity = (MainActivity) getActivity(); 
    settings = mainActivity.getSettings(); 
    selectedIP = settings.getSelectedIP(); 
    editText = (EditText) dialogView.findViewById(R.id.edit_text_ip); 
    ipSpinner = (Spinner) dialogView.findViewById(R.id.spinner_ip); 
    ImageButton imageButton = (ImageButton) dialogView.findViewById(R.id.image_button_save_ip); 
    imageButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String IP = editText.getText().toString(); 
      settings.setIP(selectedIP, IP); 
      settings.settingsSave(); 
      mainActivity.showToast("IP at " + Integer.toString(selectedIP) + " with value" + IP); 
     } 
    }); 
    ipSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
      selectedIP = position; 
      editText.setText(settings.getIP(selectedIP)); 
      mainActivity.showToast(Integer.toString(position)); 
      settings.settingsSave(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parentView) { 
      return; 
     } 

    }); 
    builder.setView(inflater.inflate(R.layout.dialog_settings, null)) 

      .setPositiveButton("Save", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        String IP = editText.getText().toString(); 
        settings.setIP(selectedIP, IP); 
        settings.settingsSave(); 
        mainActivity.showToast("IP at " + Integer.toString(selectedIP) + " with value" + IP); 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        SettingsDialog.this.getDialog().cancel(); 
       } 
      }); 
    return builder.create(); 
} 

}

和我的對話XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:padding="16dp"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="IP-address" 
    android:textAllCaps="true" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Spinner 
     android:id="@+id/spinner_ip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:entries="@array/spinner_ip_visual" 
     android:entryValues="@array/spinner_ip_values" /> 

    <EditText 
     android:id="@+id/edit_text_ip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="8" /> 

    <ImageButton 
     android:id="@+id/image_button_save_ip" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@null" 
     android:src="@drawable/ic_done_black_24dp"/> 
</LinearLayout> 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="unit" 
    android:textAllCaps="true" /> 

<RadioGroup 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <RadioButton 
     android:id="@+id/button_unit_celsius" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:text="Celsius" 
     android:textAllCaps="true" /> 

    <RadioButton 
     android:id="@+id/button_unit_fahrenheit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Fahrenheit" 
     android:textAllCaps="true" /> 
</RadioGroup> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="onButtonClickGenerateRandomData" 
    android:text="Generate random data" 
    android:textAllCaps="true" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="onButtonClickDropDatabase" 
    android:text="delete weatherdata" 
    android:textAllCaps="true" /> 

感謝諮詢

回答

2

您正在膨脹dialogView,然後你爲dialogView中的視圖設置監聽器,但是當你使用等對話框構建器的視圖,則會誇大新視圖。

這是錯誤的。

builder.setView(inflater.inflate(R.layout.dialog_settings, null)) 

這是正確的

builder.setView(dialogView) 
相關問題