我想知道我是否可以冻结当前活动,同时等待另一个活动或对话(任何可以)完成。
我知道我可以为结果启动一个活动,并在那里处理它们,但是startActivityForResult()之后的代码仍会被执行
这是我想做的事情:
PopupDialog dialog = new PopupDialog(this,android.R.style.Theme_Black_NoTitleBar);
dialog.show();
// wait here, and continue the code after the dialog has finishes
int result = getResultFromDialogSomehow();
if (result == 1){
//do something
}else{
//do something else
}
我知道这听起来很奇怪,但如果有人能告诉我如何实现这样的功能,我真的很感激。
答案 0 :(得分:9)
您也可以使用onActivityResult
在您的主要活动中呼叫
startActivityForResult(intent, 1); //here 1 is the request code
在您的Dialog类中
Intent intent = new Intent();
intent.putExtra(....) //add data if you need to pass something
setResult(2,intent); //Here 2 result code
所以你的主要活动
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == 2 && requestCode ==1){
//do something
}else{
//do something else
}
}
答案 1 :(得分:9)
在对话框中如果您想要对话框结果,那么您的方式不是获得对话结果的正确方法。 而不是你在Android中的方式,有一个回调方法,可以在您选择对话框按钮后执行
例如
AlertDialog.Builder builder = new AlertDialog.Builder(getDialogContext());
builder.setMessage("Message");
builder.setPositiveButton("Yes", new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(this, "Yes", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
builder.setNegativeButton("No", new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(this, "Yes", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
builder.show();
运行此代码时,onClick
方法中的代码将不会执行,但是当您单击对话框的按钮时它将执行。当您单击对话框上的任何按钮时,您将收到回调方法onClick
,它将执行。
答案 2 :(得分:3)
我在Dialog中使用回调来返回用户选择的字符串或值。
即。在对话框中实现一个接口
答案 3 :(得分:1)
尝试给对话框一个按钮,通过对您活动中的某些内容进行方法调用来实现onClickListener
。所述方法中的代码仅在单击按钮时运行,因此您需要使用不同的按钮参数调用该方法,具体取决于它们的作用。
答案 4 :(得分:1)
您可以使用onActivityResult
。
这是我的代码。
开始活动时。 例如,你可以从MainActivity中调用TestActivity,你可以这样做。
Constants.REQUEST_CODE = 1000; // this is a global variable...and it must be a unique.
....
Intent intent = new Intent(this, TestActivity.class);
startActivityForResult(intent, Constants.REQUEST_CODE);
。
public void onClickCancel() {
setResult(Activity.RESULT_CANCELED);
finish();
}
public void onClickConfirm() {
setResult(Activity.RESULT_OK);
finish();
}
当您在MainActivity上获得结果代码时,您可以这样做。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// todo something...
} else if (requestCode == Constants.REQUEST_CODE && resultCode == Activity.RESULT_CANCELED) {
// todo something...
}
}
答案 5 :(得分:1)
对于那些为了实现对话框而获得结果的人,但是不使用而不使用 onActivityResult 的情况是这里的使用回调的示例。这样,您可以在任何地方调用此自定义对话框,并根据选择执行某些操作。
短路
public void getDialog(Context context, String title, String body,
DialogInterface.OnClickListener listener){
AlertDialog.Builder ab = new AlertDialog.Builder(context);
ab
.setTitle(title)
.setMessage(body)
.setPositiveButton("Yes", listener)
.setNegativeButton("Cancel", listener)
;//.show();
Dialog d=ab.create();
d.setCanceledOnTouchOutside(false);
d.show();
}
private void showDialog(){
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//DO
break;
case DialogInterface.BUTTON_NEGATIVE:
//DO
break;
}
}
};
getDialog(
this,
"Delete",
"Are you sure to delete the file?",
dialogClickListener
);
}
另一种方法,适用于必须实现对话框的不同变体的情况,因为您可以在一个位置定义所有动作。
MyDialog.java
public class MyDialog{
public void deleteDialog(Context context){
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
if(listener!=null)
listener.onDeleteDialogResponse(true);
break;
case DialogInterface.BUTTON_NEGATIVE:
if(listener!=null)
listener.onDeleteDialogResponse(false);
break;
}
}
};
AlertDialog.Builder ab = new AlertDialog.Builder(context);
ab.setMessage("Are you sure to delete?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("Cancel", dialogClickListener)
.show();
}
/** my listner */
public interface MyDialogListener{
public void onDeleteDialogResponse(boolean respononse);
}
private MyDialogListener listener;
public void setListener(MyDialogListener listener) {
this.listener = listener;
}
}
以这种方式使用
private void showDialog(){
MyDialog dialog=new MyDialog();
dialog.setListener(new MyDialog.MyDialogListener() {
@Override
public void onDeleteDialogResponse(boolean respononse) {
if(respononse){
//toastMe("yessss");
//DO SOMETHING IF YES
}else{
//toastMe("noooh");
//DO SOMETHING IF NO
}
}
});
dialog.deleteDialog(this);
}
答案 6 :(得分:0)
我也不完全明白。如果你想从活动中捕获结果,那么你可以简单地开始你的menitoned“startActivityForResult”函数。如果你想在对话框中捕获一些结果,那么你可以简单地将所有函数(在按下对话框后按下后继续)放到对话框上每个按钮的onClickListener上
答案 7 :(得分:0)
我回答这个问题已经晚了几年,但是这是我的答案。
我有一个代表表单/文件的类。它有一个公共成员“deleteDialog()”,允许按需处理文件,并向调用者返回“true”或“false”值。
这是它的样子:
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
public class Form {
private Context mContext;
private CharSequence mFilePath;
private boolean mDeleted = false; // Set to "true" if this file is deleted.
/*...etc etc...*/
public boolean deleteDialog() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
//@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Do your Yes progress
mDeleted = mContext.deleteFile(mFilePath.toString());
break;
case DialogInterface.BUTTON_NEGATIVE:
//Do your No progress
mDeleted = false;
break;
}
}
};
AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
ab.setMessage("Are you sure to delete?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("Cancel", dialogClickListener)
.show();
return mDeleted;
}
您将看到结果变量 - “mDeleted” - 必须是封闭类的成员;这是由于奇怪但奇妙的Java变幻莫测。其中内部类(在本例中为“DialogInterface.OnClickListener dialogClickListener”)可以继承其外部类的状态。