2

我試圖按照this教程實現自定義DialogFragment。我的問題是我無法處理我的自定義視圖的button.setOnClickListener事件。最奇怪的部分是我沒有任何問題,我的按鈕的.getText(),我只是找不到處理點擊事件的方式。貝婁是我的代碼:View.OnClickListener在自定義DialogFragment中

SettingsDialogFragment.java

public class SettingsDialogFragment extends DialogFragment 
{ 
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) 
{ 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    // Get the layout inflater 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 
    final View view = inflater.inflate(R.layout.dialog_settings, null); 

    final Button colorButton =(Button) view.findViewById(R.id.colorButton_dialogSettings); 
    String s = colorButton.getText().toString(); 
    System.out.println("its working "+s); 

    //NOT working 
    colorButton.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      System.out.println("OnClick"); 
     } 
    }); 

    // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    builder.setView(inflater.inflate(R.layout.dialog_settings, null)) 
      // Add action buttons 
      .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) 
       { 

       } 
      }) 
      .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        SettingsDialogFragment.this.getDialog().cancel(); 
       } 
      }); 
    return builder.create(); 
} 

` 我的自定義視圖代碼(dialog_settings.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="200dp" 
      android:layout_height="wrap_content"> 
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:gravity="center" 
    android:scaleType="center" 
    android:background="#00CCCC" 
    android:contentDescription="@string/app_name" 
    android:text="@string/dialog_settings_title" 
    android:id="@+id/editText"/> 
<Button 
    android:id="@+id/colorButton_dialogSettings" 
    android:inputType="textEmailAddress" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/color_picker_title" 
    android:layout_below="@+id/editText" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    /> 

<RadioButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Stroke" 
    android:layout_marginLeft="55dp" 
    android:id="@+id/radioButtonStroke" 
    android:checked="false" 
    android:layout_below="@+id/colorButton_dialogSettings" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"/> 

<RadioButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Fill" 
    android:id="@+id/radioButton_fill" 
    android:checked="false" 
    android:layout_below="@+id/colorButton_dialogSettings" 
    android:layout_toRightOf="@+id/radioButtonStroke" 
    android:layout_toEndOf="@+id/radioButtonStroke" 
    android:layout_marginLeft="10dp" 
    /> 

+0

嘗試更改爲'colorButton.setOnClickListener(new DialogInterface.OnClickListener()' – Johnny

+0

由於DialogFragment沒有實現它,因此無法使用DialogInterface.OnClickListener [link](http://developer.android.com/reference/ android/app/DialogFragment.html) –

回答

6

我只是顯示你的重要組成部分。我希望你能找到各自的行代碼中的

final View view = inflater.inflate(R.layout.dialog_settings, null); 
// inflating your view..for drawback, this line is [A] 

colorButton已經到view.findViewById(R.id.colorButton_dialogSettings)參考這是..這你引用一個onclick偵聽器的ViewGroup view它..

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

此代碼在這裏設置您的對話框的內容視圖。它膨脹一個佈局和做它的工作..所以你builder沒有引用它的內容視圖view而是一個新的充氣R.layout.dialog_settings佈局結束..

所以要解決這個問題只是這樣做

builder.setView(view) // hope you know the view parameter 

view是你行實例化什麼[A] ..

希望我是有道理的,並足夠清晰的you..let我知道,如果它可以幫助

+1

我不敢相信我錯過了這個...謝謝! –

0

新建答案

變化你的onCreateDialog這樣:

import android.view.View.OnClickListener; 

public class SettingsDialogFragment extends DialogFragment implements onClickListener 
{ 
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) 
{ 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    // Get the layout inflater 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 
    final View view = inflater.inflate(R.layout.dialog_settings, null); 

    final Button colorButton =(Button) view.findViewById(R.id.colorButton_dialogSettings); 
    String s = colorButton.getText().toString(); 
    System.out.println("its working "+s); 

    colorButton.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      switch (v.getId()) { 
       case R.id.colorButton_dialogSettings 
        System.out.println("OnClick"); 
        break; 
       default: 
        break; 
     } 
    }); 

    // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    builder.setView(inflater.inflate(R.layout.dialog_settings, null)) 
      // Add action buttons 
      .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) 
       { 

       } 
      }) 
      .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        SettingsDialogFragment.this.getDialog().cancel(); 
       } 
      }); 
    return builder.create(); 
} 

在你的活動:

private Button colorButton = (Button) findViewById(R.id.colorButton_dialogSettings);

**舊的答案,需要你寫你自己的showDialog method* *

嘗試刪除按鈕的代碼在onCreateDialog和加入:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment_dialog, container, false); 
     Button colorButton =(Button) v.findViewById(R.id.colorButton_dialogSettings); 
      public void onClick(View v) { 
      // When button is clicked, call up to owning activity. 
       ((FragmentDialog)getActivity()).showDialog(); 
       System.out.println("OnClick"); 
      } 
     }); 

     return v; 
相關問題