如果目标操作失败,如何在按钮中显示警告对话框 [英] how to display alert dialog box in button if target action fails

查看:71
本文介绍了如果目标操作失败,如何在按钮中显示警告对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向下投票最爱





我正在尝试创建一个应用程序,用户可以拍摄照片并将其设置为壁纸。现在,当我单击按钮并返回而不拍照并点击设置壁纸时,我想显示一个警告对话框。我可以在未启动图像捕获时设置它但在启动时没有设置但没有拍摄图像< br $> b $ b



down vote favorite


I am trying to create an app where the user can take a photo and set it as wallpaper.Now when i click the button and get back without taking a photo and click set wallpaper i want to display an alert dialog box.I have been able to set it when the image capture is not initiated but not when initiated but no image is taken


package com.sagarapp;

import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Camera extends Activity implements View.OnClickListener{

ImageView img;
ImageButton takepic;
Button setdp;
Intent i;
final static int cameraData = 0;
Bitmap bmp;
final Context context = this;
boolean taken = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photo);
    initialize();
    takepic.setOnClickListener(this);
    setdp.setOnClickListener(this);

}

private void initialize() {
    // TODO Auto-generated method stub
    img = (ImageView)findViewById(R.id.Ivpic);
    takepic = (ImageButton)findViewById(R.id.Ibcapture);
    setdp = (Button)findViewById(R.id.Bsetdp);
}

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.Ibcapture:
        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        taken = true;
        startActivityForResult(i,cameraData);
        break;
    case R.id.Bsetdp:
        try {
            if(taken)
                getApplicationContext().setWallpaper(bmp);
            else{
            AlertDialog.Builder alertDialogBulider = new AlerDialog.Builder(context);
            alertDialogBulider.setTitle("Warning!");
            alertDialogBulider.setMessage("No Image Is Available");
            alertDialogBulider.setCancelable(false);
  alertDialogBulider.setNeutralButton("OK", new   DialogInterface.OnClickListener()   {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // TODO Auto-generated method stub
                    dialog.cancel();
                }
            });
            AlertDialog alertDialog = alertDialogBulider.create();
            alertDialog.show();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
        break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK)
    {
        Bundle extras = data.getExtras();
        bmp = (Bitmap)extras.get("data");
        img.setImageBitmap(bmp);
    }
    else
    {
        AlertDialog.Builder alertDialogBulider = new AlertDialog.Builder(context);
        alertDialogBulider.setTitle("Warning!");
        alertDialogBulider.setMessage("No Photo Is Taken");
        alertDialogBulider.setCancelable(false);
        alertDialogBulider.setNeutralButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int id) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }
        });
        AlertDialog alertDialog = alertDialogBulider.create();
        alertDialog.show();

    }
}
}

推荐答案

你所采用的布尔变量应该只是如果在onActivityResult()方法中收到RESULT_OK结果代码,则设置为true。这意味着用户实际拍了照并保存了。从R.id.Ibcapture View onClick()方法中删除它。
Your taken boolean variable should only be set to true if you receive a RESULT_OK resultcode in onActivityResult() method. That means that the user actually took a picture and saved it. Remove it from the R.id.Ibcapture View onClick () method.


这篇关于如果目标操作失败,如何在按钮中显示警告对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆