我想实现一个显示对话框的方法,等待对话框解除,然后根据对话框内容返回结果.这可能吗?
public String getUserInput()
{
//do something to show dialog
String input = //get input from dialog
return input;
}
Run Code Online (Sandbox Code Playgroud)
我实际上是在尝试实现一个具有方法"public String getUserInput()"的接口,其中必须通过对话框检索返回的String.这很容易在java中完成,在android中似乎不可能?
编辑:根据评论中的要求发布一些示例代码
getInput()
必须从后台线程调用(我从AsynchTask中调用它).getInput()显示一个对话框并调用wait.在对话框上按下确定按钮时,对话框在成员变量中设置用户输入并调用notify.调用notify时,getInput()继续并返回成员变量.
String m_Input;
public synchronized String getInput()
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
AlertDialog.Builder alert = new AlertDialog.Builder(context);
//customize alert dialog to allow desired input
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
m_Input = alert.getCustomInput();
notify();
}
});
alert.show();
}
});
try
{
wait();
}
catch (InterruptedException e) …
Run Code Online (Sandbox Code Playgroud) 我为Android创建了一个简单的游戏; 我想通过意外按下主页或后退按钮来阻止用户将游戏发送到后台.我计划使用对话框来确认用户的操作.我想要的是类似于Sirius对他们的应用程序所做的,如果你需要一个插图.
我尝试将此操作添加到重写的onPause(),但是在显示对话框后,活动立即消失,因此我认为我不会以正确的方式进行此操作.我怎样才能解决这个问题?
任何帮助表示赞赏.
我正在开发一个使用Android设备的后退按钮的应用程序.我想知道如何检查活动是否是最后一个,如果用户再次按下它,它不会退出应用程序,除非用户再次按下,就像9gag应用程序一样.
谢谢!
我想留言"你确定要退出吗?" 当我按下"BACK"按钮时,我的应用程序中的"是" - "否"..这可能吗?我怎么办呢?谢谢