gpt4 book ai didi

java - 如果目标操作失败,如何在按钮中显示警报对话框

转载 作者:太空宇宙 更新时间:2023-11-04 14:36:52 24 4
gpt4 key购买 nike

我正在尝试创建一个应用程序,用户可以在其中拍照并将其设置为壁纸。现在,当我单击按钮并返回而不拍照并单击设置壁纸时,我想显示一个警报对话框。我未启动图像采集时可以设置,但启动但未采集图像时则不能设置

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 结果代码时,您的 taken boolean 变量才应设置为 true > 方法。这意味着用户实际上拍摄了照片并保存了它。从 R.id.Ibcapture View onClick () 方法中删除它。

关于java - 如果目标操作失败,如何在按钮中显示警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25462324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com