我的首选项活动中有一个对话框,定义如下(布局以xml定义):
public class DialogPreference extends android.preference.DialogPreference {
public DialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DialogPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void onClick (DialogInterface dialog, int which) {
if (DialogInterface.BUTTON_POSITIVE == which) {
if (getKey() ==
getContext().getResources().getText(R.string.blabla)) {
// FIXME ??? - how ?
Log.v(TAG, "DialogPreference: onClick");
}
}
}
}
在对话框中有几个小部件,即RadioGroup
和几个RadioButton
。我目前无法在onClick
方法中找到访问这些小部件的方法。
访问这些小部件的方法是什么?
答案 0 :(得分:2)
您可以将DialogInterface
转换为AlertDialog
,如下所示:
public void onClick (DialogInterface dialog, int which) {
if (DialogInterface.BUTTON_POSITIVE == which) {
RadioButton button = (RadioButton) ((AlertDialog) dialog).findViewById(R.id.radio_button_id);
// continue using button
}
}
(或类似的东西)
答案 1 :(得分:1)
下面是我的整个代码我是如何做到的
final AlertDialog.Builder Main_Dialog = new AlertDialog.Builder(this);
////This is how to set layout
final View layout = getLayoutInflater().inflate(R.layout.conpassword,null);
Main_Dialog.setTitle("Choose reminder");
Main_Dialog.setView(layout);
Main_Dialog.setTitle("Enter IP Address:");
Main_Dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
connectWIFi();
if (connect && ni.getType() == ConnectivityManager.TYPE_WIFI)
{
final ImageButton input = (ImageButton) layout.findViewById(R.id.ImageButton01);
saveIP(input.getText().toString(), deviceId);
Thread cThread = new Thread(new ClientServer(LoginScreen.this, input.getText().toString(),
deviceId, mHandler));
cThread.start();
dialog.dismiss();
}
else
{
Builder builder = new AlertDialog.Builder(LoginScreen.this);
builder.setTitle("Alert !");
builder.setMessage("No WiFi connection. Please check your WiFi settings");
builder.setPositiveButton("Ok", null);
builder.show();
}
}
});
Main_Dialog.show();
我不知道为什么你没有得到getLayoutInflater,但这段代码与我一起顺利运行