2013-12-17 51 views
0

除非按下ImageView(img_log)並單擊對話框,否則我的按鈕(登錄)不起作用。我不希望我的登錄按鈕被禁用。請幫我調試我的代碼的對話框功能已經完成Android:按鈕被禁用,僅在執行對話框功能後才起作用

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(); 
     } 

    }); 

回答

1

移動下面的代碼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(); 
    } 

}); 
+0

感謝它的工作! – SlopTonio