是的,我知道有AlertDialog.Builder,但我很震惊地知道在Android中显示对话框有多困难(好吧,至少不是程序员友好).
我曾经是一名.NET开发人员,我想知道是否有以下的Android相当于?
if (MessageBox.Show("Sure?", "", MessageBoxButtons.YesNo) == DialogResult.Yes){
// Do something...
}
Run Code Online (Sandbox Code Playgroud) 这些天我正在研究在Android中模拟模态对话框.我已经google了很多,有很多讨论,但很遗憾没有太多的选择来获得它的模态.这里有一些背景,
对话框,模态对话框和Blockin
对话框/ AlertDialogs:如何在对话框启动时"阻止执行"(.NET风格)
没有直接的方法来获得模态行为,然后我想出了3个可能的解决方案,
1.使用对话框主题的活动,就像这个线程所说的那样,但我仍然无法使主要活动真正等待对话活动返回.主要活动转为停止状态,然后重新启动.
2.构建一个工作线程,并使用线程同步.但是,对于我的应用程序来说,这是一个巨大的重构工作,现在我在主UI线程中有一个主要活动和服务.
3.当存在模态对话框时,在循环内接管事件处理,并在对话框关闭时退出循环.实际上,这是构建一个真正的模态对话框的方式,就像它在Windows中的确切做法一样.我仍然没有这样的原型.
我仍然想用一个以对话为主题的活动模拟它,
1.通过startActivityForResult()启动对话活动
2.从onActivityResult()获取结果
这里有一些来源
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView v = new MyView(this);
setContentView(v);
}
private final int RESULT_CODE_ALERT = 1;
private boolean mAlertResult = false;
public boolean startAlertDialog() {
Intent it = new Intent(this, DialogActivity.class);
it.putExtra("AlertInfo", "This is an alert");
startActivityForResult(it, RESULT_CODE_ALERT);
// I want to wait right here
return mAlertResult;
}
@Override
protected void onActivityResult (int requestCode, int resultCode, …Run Code Online (Sandbox Code Playgroud) 我正在开发一个与我编写的RESTful Web服务进行通信的Android应用程序.使用Volleyfor GET方法非常简单,但我不能指责POST方法.
我想在POST请求String正文中发送请求,并检索Web服务的原始响应(如200 ok,500 server error).
所有我能找到的是StringRequest不允许发送数据(正文),并且它限制我接收解析的String响应.我也遇到了JsonObjectRequest接受数据(正文)但检索解析后的JSONObject响应.
我决定编写自己的实现,但是我找不到从Web服务接收原始响应的方法.我该怎么做?
我试图用OK按钮显示一个消息框.我为此目的使用AlertDialog,我意识到它没有阻止代码.例:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 1")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
})
.setNegativeButton("", null)
.show();
new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 2")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
})
.setNegativeButton("", null)
.show();
//...continue with UI initialization here...
}
Run Code Online (Sandbox Code Playgroud)
当我开始活动时,它会显示Alert2,当我按下确定后,它会显示Alert1.
我需要有阻止代码对话框,所以首先它应该显示Alert1消息,等到用户按下OK按钮然后继续执行代码并显示Alert2消息等.示例:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
msgBox("Test dlg", "Alert 1");
msgBox("Test dlg", "Alert 2");
//...continue with UI initialization here...
}
private void msgBox(String …Run Code Online (Sandbox Code Playgroud) 我的代码在这里:
public static boolean showConfirmationDialog(Context context, String title, String dialogContent) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setTitle(title);
builder.setMessage(dialogContent);
builder.setPositiveButton("Confirm", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// what to do ?
}
});
Run Code Online (Sandbox Code Playgroud)
现在,我想点击"确认"按钮后返回true.那么如何从内部类返回"true" - 方法的OnClickListener.
需要一些帮助,谢谢.
我想询问用户是否同意继续启动任务,如何发出具有Yes,No按钮的确认窗口?