Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么意图在AlertDialog.Builder之前启动,即使它是以相反的方式编码的_Android_Android Activity_Android Intent_Android Alertdialog - Fatal编程技术网

Android 为什么意图在AlertDialog.Builder之前启动,即使它是以相反的方式编码的

Android 为什么意图在AlertDialog.Builder之前启动,即使它是以相反的方式编码的,android,android-activity,android-intent,android-alertdialog,Android,Android Activity,Android Intent,Android Alertdialog,使用以下代码,调用newPicture时会启动意图,随后会显示对话框。这意味着什么?我如何更改订单 public void newPicture(View v) { SharedPreferences settings = getPreferences(MODE_PRIVATE); boolean geoProtipAlreadyShown = settings.getBoolean("geoProtipAlreadyShown", false); if (!geoPr

使用以下代码,调用newPicture时会启动意图,随后会显示对话框。这意味着什么?我如何更改订单

public void newPicture(View v) {
    SharedPreferences settings = getPreferences(MODE_PRIVATE);
    boolean geoProtipAlreadyShown = settings.getBoolean("geoProtipAlreadyShown", false);

    if (!geoProtipAlreadyShown) {
        showGeoProtip();

        // and set the option in SharedPreferences
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("geoProtipAlreadyShown", true);
        editor.commit();
    }

    // start the image capture activity
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(PATH, "tmpfile.jpg")));
    startActivityForResult(intent, IMAGE_CAPTURE);  

}

private void showGeoProtip() {
    String geoProtip = this.getResources().getString(R.string.protip);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(geoProtip).setCancelable(true).setPositiveButton("OK", null);
    AlertDialog alert = builder.create();
    alert.show();
}

将您的意图发送移动到对话框按钮之一的onclicklistener。

将您的意图发送移动到对话框按钮之一的onclicklistener。

这是Android程序员的经典窍门。基本上,显示警报不会停止代码的执行,因此您必须在onclicklistener中启动intent。

这是Android程序员的经典窍门。基本上,显示警报不会停止代码的执行,因此您必须在onclicklistener中启动intent。

将开始图像捕获活动移动到新方法,并将其放置到dialog的onclicklistener:

builder.setMessage(geoProtip).setCancelable(true).setPositiveButton("OK",
    new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            captureImage();
        }
    });


private void captureImage(){
        // start the image capture activity
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(PATH, "tmpfile.jpg")));
        startActivityForResult(intent, IMAGE_CAPTURE);        
}
如果还有,请修改:

if (!geoProtipAlreadyShown) {
    showGeoProtip();
    ....
}else{
    captureImage();
}

将“开始图像捕获”活动移动到新方法,并将其放入对话框的OnClickListener:

builder.setMessage(geoProtip).setCancelable(true).setPositiveButton("OK",
    new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            captureImage();
        }
    });


private void captureImage(){
        // start the image capture activity
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(PATH, "tmpfile.jpg")));
        startActivityForResult(intent, IMAGE_CAPTURE);        
}
如果还有,请修改:

if (!geoProtipAlreadyShown) {
    showGeoProtip();
    ....
}else{
    captureImage();
}

我认为这将是有用的

Dialog dlg = new AlertDialog.Builder(context)
    .setTitle("TITLE")
    .setMessage("MSG")
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //Write the intent here.
        }
    })
    .create();
    dlg.show();

我认为这将是有用的

Dialog dlg = new AlertDialog.Builder(context)
    .setTitle("TITLE")
    .setMessage("MSG")
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //Write the intent here.
        }
    })
    .create();
    dlg.show();

问题在于,警报只应在第一次启动时显示一次。那么,当对话框未显示时,如何启动意图?我是否必须将意图代码导出到一个单独的方法,并通过对话框和else语句调用它?问题是,警报只应在第一次启动时显示一次。那么,当对话框未显示时,如何启动意图?我是否必须将意图代码导出到一个单独的方法,并通过对话框和else语句调用它?