我試圖按照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"
/>
嘗試更改爲'colorButton.setOnClickListener(new DialogInterface.OnClickListener()' – Johnny
由於DialogFragment沒有實現它,因此無法使用DialogInterface.OnClickListener [link](http://developer.android.com/reference/ android/app/DialogFragment.html) –