请参阅我已经更新了帖子
我可以设置成变量像Web开发会话环境。
下面是我的code,其中我正在开发一个确认框,只要android应用上手
Here is my code to in which I am developing an confirmation box as soon as the android application get started
package com.example.alertboxandloadingwidgets;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Boolean result = showConfirmationBox("Are you sure you want to do this",
this);
}
public Boolean showConfirmationBox(String messageToShow, final Context context) {
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
// set the message to display
alertbox.setMessage(messageToShow);
// set a positive/yes button and create a listener
alertbox.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(context,
"'Yes' button clicked", Toast.LENGTH_SHORT)
.show();
}
});
// set a negative/no button and create a listener
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(context, "'No' button clicked",
Toast.LENGTH_SHORT).show();
}
});
// display box
alertbox.show();
}
}
但我想,如果点击了是
按钮,那么它必须返回真正
,如果没有
按钮被点击,然后它返回假
。
But I want that if the yes
button is clicked then it has to return true
and if no
button is clicked then it has to return false
.
但我不能这样做,因为返回类型 onClickListener
是无效的。
But I am not able to do so because return type of onClickListener
is void.
请帮我我能为这个做的。
Please help me what can I do for this.
但问题是,我有让通用方法这个方法我有一个CommonUtilities类写出从何处的任何活动都可以使用这种方法。所以,我必须设置或重置价值从那里我调用此方法的结果参数。的
But the problem is that I have make it generic means This method I have to write in a CommonUtilities Class From where any of the activity can use this method. So I have to set or reset the value the result parameter from where I am calling this method.
Android的对话框是异步的,因此,你需要重构你的code来处理这个问题。我猜你是打算做这样的事情:
Android dialogs are asynchronous, therefore you need to refactor your code to deal with this. I'm guessing you were planning to do something like this:
boolean result = showConfirmation(...);
if(result) {
//do something
}
else {
//do something else
}
您可以实现像这样同样的结果:
You can achieve the same result with something like this:
public class MainActivity extends Activity {
private boolean result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showConfirmationBox("Are you sure you want to do this", this);
}
private doOnTrueResult() {
result = true;
//do something
}
private doOnFalseResult() {
result = false;
//do something else
}
public void showConfirmationBox(String messageToShow, final Context context) {
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
// set the message to display
alertbox.setMessage(messageToShow);
// set a positive/yes button and create a listener
alertbox.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(context,
"'Yes' button clicked", Toast.LENGTH_SHORT)
.show();
doOnTrueResult();
}
});
// set a negative/no button and create a listener
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(context, "'No' button clicked",
Toast.LENGTH_SHORT).show();
doOnFalseResult();
}
});
// display box
alertbox.show();
}
}