gpt4 book ai didi

java - Android:按钮被禁用,仅在执行对话框功能后才起作用

转载 作者:行者123 更新时间:2023-12-01 13:42:21 27 4
gpt4 key购买 nike

除非按下 ImageView (img_log) 并单击对话框,否则我的按钮(登录)不起作用。我不想禁用我的登录按钮。请帮我调试我的代码,以找出为什么我的登录按钮只有在对话框功能完成后才起作用

public class Register extends Activity {

/**
* JSON Response node names.
**/


private static String KEY_SUCCESS = "success";
private static String KEY_UID = "uid";
private static String KEY_FIRSTNAME = "fname";
private static String KEY_LASTNAME = "lname";
private static String KEY_USERNAME = "uname";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
private static String KEY_ERROR = "error";

/**
* Defining layout items.
**/

EditText inputFirstName;
EditText inputLastName;
EditText inputUsername;
EditText inputEmail;
EditText inputPassword;
Button btnRegister;
TextView registerErrorMsg;


/**
* Called when the activity is first created.
*/


public static int RESULT_LOAD_IMAGE = 1;
ImageView img_logo;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
private Intent pictureActionIntent = null;
Bitmap bitmap;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);

/**
* Defining all layout items
**/
inputFirstName = (EditText) findViewById(R.id.fname);
inputLastName = (EditText) findViewById(R.id.lname);
inputUsername = (EditText) findViewById(R.id.uname);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.pword);
btnRegister = (Button) findViewById(R.id.register);
registerErrorMsg = (TextView) findViewById(R.id.register_error);





img_logo= (ImageView) findViewById(R.id.profilepic);
img_logo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startDialog();
}

});
}

private void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("Upload Pictures Option");
myAlertDialog.setMessage("How do you want to set your picture?");

myAlertDialog.setPositiveButton("Gallery",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
pictureActionIntent = new Intent(
Intent.ACTION_GET_CONTENT, null);
pictureActionIntent.setType("image/*");
pictureActionIntent.putExtra("return-data", true);
startActivityForResult(pictureActionIntent,
GALLERY_PICTURE);
}
});

myAlertDialog.setNegativeButton("Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
pictureActionIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(pictureActionIntent,
CAMERA_REQUEST);

}
});
myAlertDialog.show();
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICTURE) {
if (resultCode == RESULT_OK) {
if (data != null) {
// our BitmapDrawable for the thumbnail
BitmapDrawable bmpDrawable = null;
// try to retrieve the image using the data from the intent
Cursor cursor = getContentResolver().query(data.getData(),
null, null, null, null);
if (cursor != null) {

cursor.moveToFirst();

int idx = cursor.getColumnIndex(ImageColumns.DATA);
String fileSrc = cursor.getString(idx);
bitmap = BitmapFactory.decodeFile(fileSrc); // load
Bitmap originalBitmap = BitmapFactory.decodeFile(fileSrc); // preview
Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, img_logo.getWidth(), img_logo.getHeight(), false); // image

// bmpDrawable = new BitmapDrawable(bitmapPreview);
img_logo.setImageBitmap(scaledBitmap);
} else {

bmpDrawable = new BitmapDrawable(getResources(), data
.getData().getPath());
img_logo.setImageDrawable(bmpDrawable);
}

} else {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
if (data.hasExtra("data")) {

// retrieve the bitmap from the intent
bitmap = (Bitmap) data.getExtras().get("data");

// Bitmap originalBitmap = BitmapFactory.decodeFile(fileSrc); // preview
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, img_logo.getWidth(), img_logo.getHeight(), false); // image

// bmpDrawable = new BitmapDrawable(bitmapPreview);
img_logo.setImageBitmap(scaledBitmap);
} else if (data.getExtras() == null) {

Toast.makeText(getApplicationContext(),
"No extras to retrieve!", Toast.LENGTH_SHORT)
.show();

BitmapDrawable thumbnail = new BitmapDrawable(
getResources(), data.getData().getPath());

// update the image view with the newly created drawable
img_logo.setImageDrawable(thumbnail);

}

} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
}




/**
* Button which Switches back to the login screen on clicked
**/

Button login = (Button) findViewById(R.id.bktologin);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Login.class);
startActivityForResult(myIntent, 0);
finish();
}

});

最佳答案

将以下代码移至 onCreate() 方法中。现在它位于 onActivityResult() 中,这就是它无法按预期工作的原因:

Button login = (Button) findViewById(R.id.bktologin);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Login.class);
startActivityForResult(myIntent, 0);
finish();
}

});

关于java - Android:按钮被禁用,仅在执行对话框功能后才起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20625056/

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