0

我的 android 项目的 2 个类中有相同的代码。我刚刚开始学习java / android,所以请你给我一些提示吗?我是否设置了新课程?感谢您的帮助!

主.java

public class Main extends Activity {


private static int RESULT_LOAD_IMAGE = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnGallery = (Button) findViewById(R.id.btnGallery);
    btnGallery.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
}

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
    Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor cursor = getContentResolver().query(selectedImage,
            filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);

    Intent uploadActivity = new Intent(Main.this, Upload.class);
    uploadActivity.putExtra("picturePath", picturePath);
    startActivity(uploadActivity);
    cursor.close();
}


 }
 }

和 Upload.java

  public class Upload extends Activity {

private static int RESULT_LOAD_IMAGE = 1;

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload);
    Bundle extras = getIntent().getExtras();
    String picturePathView = extras.getString("picturePath");
    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    findViewById(R.id.imgView).setScrollBarStyle(
            View.SCROLLBARS_INSIDE_INSET);
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePathView));

    Button btnGallery = (Button) findViewById(R.id.btnGallery);
    btnGallery.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        findViewById(R.id.imgView).setScrollBarStyle(
                View.SCROLLBARS_INSIDE_INSET);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        cursor.close();
    }

}
  }
4

3 回答 3

1

这实际上是 hasanghaforian 所说的,但更准确一点。

您启用的抽象 Activity 包含与 Upload 和 Main 相同的属性可能如下

public abstract class AbstractMediaPickerActivity extends Activity {

    protected static int RESULT_LOAD_IMAGE = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected void startMediaPicker() {
        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }

    protected abstract void onImagePicked(String picturePath);

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);

            cursor.close();

            onImagePicked(picturePath);

        }

    }
}

您的其他两个类都由 AbstractMediaPickerActivity 继承

public class MainActivity extends AbstractMediaPickerActivity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnGallery = (Button) findViewById(R.id.btnGallery);
        btnGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                startMediaPicker();
            }
        });
    }

    protected void onImagePicked(String picturePath) {
        Intent uploadActivity = new Intent(Main.this, Upload.class);
        uploadActivity.putExtra("picturePath", picturePath);
        startActivity(uploadActivity);
    }

}


public class Upload extends AbstractMediaPickerActivity {


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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        Bundle extras = getIntent().getExtras();
        String picturePathView = extras.getString("picturePath");
        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        findViewById(R.id.imgView).setScrollBarStyle(
                View.SCROLLBARS_INSIDE_INSET);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePathView));

        Button btnGallery = (Button) findViewById(R.id.btnGallery);
        btnGallery.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                startMediaPicker();
            }
        });
    }

    @Override
    protected void onImagePicked(String picturePath) {
           ImageView imageView = (ImageView) findViewById(R.id.imgView);
            findViewById(R.id.imgView).setScrollBarStyle(
                    View.SCROLLBARS_INSIDE_INSET);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));    
    }

}
于 2013-03-23T17:03:20.070 回答
0

创建一个扩展 Activity 的类(例如 SuperActivity)并向其添加重复代码。然后创建扩展 SuperActivity 的活动 A、B。

于 2013-03-23T16:52:16.950 回答
0

当然,您可以将所有代码放在一个类中。我不会编写所有代码,但您会了解如何在代码中进行重构。

对于onClickListner,您可以执行以下操作:

@Override
public void onClick(View clickedView) {

    int clickedViewId = clickedView.getId();

    switch(clickedViewId) {
        case R.id.firstItem:
            // code when first view is clicked
        case R.id.secondItem:
            // code when second view is clicked
        default:
            // optional
    }
}

然后onClickListner为两个视图设置相同的值。

您可以在onActivityResult方法中使用相同类型的 switch 语句。我希望现在你可以将这两个类合二为一。

于 2013-03-23T16:57:34.313 回答