Android setOnClick不工作/对话框不出现
应用程序启动正常,但当我单击“选择玩家”按钮时,该对话框不会出现在我的设备上。 代码如下:Android setOnClick不工作/对话框不出现,android,onclick,dialogfragment,Android,Onclick,Dialogfragment,应用程序启动正常,但当我单击“选择玩家”按钮时,该对话框不会出现在我的设备上。 代码如下: public class MainActivity extends Activity { private Button selectPlayers; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(saved
public class MainActivity extends Activity {
private Button selectPlayers;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
super.onStart(); //customize
super.onResume(); //customize
selectPlayers = (Button) findViewById(R.id.add_players);
selectPlayers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launch dialogbox on click
onCreateDialog(savedInstanceState);
}
});
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
@SuppressWarnings("rawtypes")
final ArrayList mSelectedItems = new ArrayList(); // Where we track the selected items
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Set the dialog title
builder.setTitle(R.string.select_players)
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
.setMultiChoiceItems(R.array.players_name, null,
new DialogInterface.OnMultiChoiceClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//CODE TO CLOSE DIALOGBOX AND START FORGE
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//CODE TO JUST CLOSE DIALOGBOX
}
});
return builder.create();
}
}
我看到Dialog方法返回一个对话框,但我不确定如何使它作为onClick的结果出现?(作为参考,我从Android开发者网站上获取了对话方法。)
谢谢大家! 此方法返回一个对话框,因此您必须在onClick中构建这样一个对话框
Dialog d = onCreateDialog(savedInstanceState);
d.show();
我认为您尝试的是重写Activity方法onCreateDialog(),但必须以另一种方式执行,如下所示:
此方法返回一个对话框,因此您必须在onClick中构建这样一个对话框
Dialog d = onCreateDialog(savedInstanceState);
d.show();
我认为您尝试的是重写Activity方法onCreateDialog(),但必须以另一种方式执行,如下所示:
试试看
selectPlayers.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
// Launch dialogbox on click
onCreateDialog(savedInstanceState);
}
});
始终在视图扩展对象内使用侦听器。
我把你的代码“视图”改为“按钮”了。
我还没有测试过它,我记得像你一样做了很多,而且很有效。但我记得过去几天的一个问题,这取决于android API在收集garbadge的周Hashmaps中处理内联侦听器的方式。
如果这是你的问题,解决方法很简单。
将侦听器附加到变量并传递该变量
例如:
Button.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launch dialogbox on click
onCreateDialog(savedInstanceState);
}
};
selectPlayers.setOnClickListener(listener)
这样,您的活动将保留侦听器并将其实例传递给按钮。现在,垃圾收集器不应该删除它,直到您的活动被删除或您手动删除它
哦。。
没有看到您对“onCreateDialog”的调用。以“on”开头的第一个方法是生命周期方法,它将从系统中调用,而不是手动调用。我想onCreateDialog已经被弃用了。关于这个问题,我参考阿片剂的答案。试试看
selectPlayers.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
// Launch dialogbox on click
onCreateDialog(savedInstanceState);
}
});
始终在视图扩展对象内使用侦听器。
我把你的代码“视图”改为“按钮”了。
我还没有测试过它,我记得像你一样做了很多,而且很有效。但我记得过去几天的一个问题,这取决于android API在收集garbadge的周Hashmaps中处理内联侦听器的方式。
如果这是你的问题,解决方法很简单。
将侦听器附加到变量并传递该变量
例如:
Button.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launch dialogbox on click
onCreateDialog(savedInstanceState);
}
};
selectPlayers.setOnClickListener(listener)
这样,您的活动将保留侦听器并将其实例传递给按钮。现在,垃圾收集器不应该删除它,直到您的活动被删除或您手动删除它
哦。。
没有看到您对“onCreateDialog”的调用。以“on”开头的第一个方法是生命周期方法,它将从系统中调用,而不是手动调用。我想onCreateDialog已经被弃用了。对于这个问题,我参考了Opiatefuchs的答案。您缺少show()函数,您应该像下面那样使用它
dialog.show() 缺少show()函数,您应该像下面那样使用它
dialog.show() 首先将侦听器设置为onCreate()中的按钮 对话框的代码
public Dialog onCreateDialog(Bundle savedInstanceState) {
@SuppressWarnings("rawtypes")
final ArrayList mSelectedItems = new ArrayList(); // Where we track the selected items
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Set the dialog title
builder.setTitle(R.string.select_players)
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
.setMultiChoiceItems(R.array.players_name, null,
new DialogInterface.OnMultiChoiceClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//CODE TO CLOSE DIALOGBOX AND START FORGE
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//CODE TO JUST CLOSE DIALOGBOX
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void onClick(View arg0)
{
if(arg0== selectPlayers){
//Show your dialog
}
}
拥有一个onClick()方法意味着您可以用一个方法处理多个按钮等,使用if/case语句来区分每个按钮,这里如果arg0是用于调用对话框的按钮,那么它应该调用处理对话框的方法
public Dialog onCreateDialog(Bundle savedInstanceState) {
@SuppressWarnings("rawtypes")
final ArrayList mSelectedItems = new ArrayList(); // Where we track the selected items
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Set the dialog title
builder.setTitle(R.string.select_players)
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
.setMultiChoiceItems(R.array.players_name, null,
new DialogInterface.OnMultiChoiceClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//CODE TO CLOSE DIALOGBOX AND START FORGE
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//CODE TO JUST CLOSE DIALOGBOX
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void onClick(View arg0)
{
if(arg0== selectPlayers){
//Show your dialog
}
}
这是我个人的偏好,我觉得这样做可以保持一切整洁,但这也是我的观点。首先,将侦听器设置为onCreate()中的按钮 对话框的代码
public Dialog onCreateDialog(Bundle savedInstanceState) {
@SuppressWarnings("rawtypes")
final ArrayList mSelectedItems = new ArrayList(); // Where we track the selected items
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Set the dialog title
builder.setTitle(R.string.select_players)
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
.setMultiChoiceItems(R.array.players_name, null,
new DialogInterface.OnMultiChoiceClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//CODE TO CLOSE DIALOGBOX AND START FORGE
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//CODE TO JUST CLOSE DIALOGBOX
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void onClick(View arg0)
{
if(arg0== selectPlayers){
//Show your dialog
}
}
拥有一个onClick()方法意味着您可以用一个方法处理多个按钮等,使用if/case语句来区分每个按钮,这里如果arg0是用于调用对话框的按钮,那么它应该调用处理对话框的方法
public Dialog onCreateDialog(Bundle savedInstanceState) {
@SuppressWarnings("rawtypes")
final ArrayList mSelectedItems = new ArrayList(); // Where we track the selected items
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Set the dialog title
builder.setTitle(R.string.select_players)
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
.setMultiChoiceItems(R.array.players_name, null,
new DialogInterface.OnMultiChoiceClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//CODE TO CLOSE DIALOGBOX AND START FORGE
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//CODE TO JUST CLOSE DIALOGBOX
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void onClick(View arg0)
{
if(arg0== selectPlayers){
//Show your dialog
}
}
这是我个人的喜好,我觉得这样做可以保持一切整洁,但这只是我的观点