View.OnClickListener 在自定义 DialogFragment 中
View.OnClickListener in a custom DialogFragment
我正在按照 this 教程尝试实施自定义 DialogFragment
。我的问题是我无法处理自定义视图的 button.setOnClickListener 事件。最奇怪的是,我在获取按钮的 .getText()
时没有问题,只是找不到处理点击事件的方法。下面是我的代码:
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"
/>
新答案
将您的 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();
}
在你的 activity:
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;
我只是向您展示了重要的部分..我希望您能在代码中找到它们各自的行
final View view = inflater.inflate(R.layout.dialog_settings, null);
// inflating your view..for drawback, this line is [A]
您的 colorButton
引用了来自视图组 view
的 view.findViewById(R.id.colorButton_dialogSettings)
..您为其引用了一个 onclick 侦听器..
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
此处的这段代码设置对话框的内容视图。它使布局膨胀并起作用.. 所以最后你的 builder
没有引用它的 content view 到 view
而是一个新的膨胀 R.layout.dialog_settings
布局..
所以要解决就这样做
builder.setView(view) // hope you know the view parameter
view
是您在第 [A] 行实例化的内容..
希望我说得有道理,对你来说足够清楚..如果有帮助请告诉我
我正在按照 this 教程尝试实施自定义 DialogFragment
。我的问题是我无法处理自定义视图的 button.setOnClickListener 事件。最奇怪的是,我在获取按钮的 .getText()
时没有问题,只是找不到处理点击事件的方法。下面是我的代码:
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"
/>
新答案
将您的 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();
}
在你的 activity:
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;
我只是向您展示了重要的部分..我希望您能在代码中找到它们各自的行
final View view = inflater.inflate(R.layout.dialog_settings, null);
// inflating your view..for drawback, this line is [A]
您的 colorButton
引用了来自视图组 view
的 view.findViewById(R.id.colorButton_dialogSettings)
..您为其引用了一个 onclick 侦听器..
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
此处的这段代码设置对话框的内容视图。它使布局膨胀并起作用.. 所以最后你的 builder
没有引用它的 content view 到 view
而是一个新的膨胀 R.layout.dialog_settings
布局..
所以要解决就这样做
builder.setView(view) // hope you know the view parameter
view
是您在第 [A] 行实例化的内容..
希望我说得有道理,对你来说足够清楚..如果有帮助请告诉我