gpt4 book ai didi

android:从操作栏显示一个警告对话框

转载 作者:行者123 更新时间:2023-11-29 15:15:48 25 4
gpt4 key购买 nike

我试图在单击操作栏的项目时显示 AlertDialog,但没有任何反应!!!我是 Android 编程的新手,您能给我发一些代码或告诉我如何操作吗?

谢谢大家!!

马特奥

最佳答案

好的,首先你必须在操作栏的项目被右击时捕获事件 onclick 吗?

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == android.R.id.home) //the androi.r.id.home have to changed
//for the id of your button.
{
///here is where you have to show the alertdialog!!!!
}
}




public boolean onOptionsItemSelected(MenuItem item)
{
//This is the layout that you are going to use in your alertdialog
final View addView = getLayoutInflater().inflate(R.layout.add, null);

new AlertDialog.Builder(this).setTitle("Add a Word").setView(addView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
addWord((TextView) addView.findViewById(R.id.title));
}
}).setNegativeButton("Cancel", null).show();

return (super.onOptionsItemSelected(item));
}

在这里获取完整的源代码表格..

http://vimaltuts.com/android-tutorial-for-beginners/android-action-bar-tab-menu-example

您也可以像这样构建您的 AlertDialgog:

new AlertDialog.Builder(this)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();

关于android:从操作栏显示一个警告对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24055625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com