如何制作多个onClick()的方法?

时间:2018-10-01 15:38:02

标签: java android

我的android应用具有三个按钮。单击所有按钮都可以完成相同的工作, 而且代码太长了,我不想将代码复制3次。 那么,我该如何使用一种方法呢? 这是一个按钮的代码

 public void onClick(View v) {
    switch (v.getId()) {

            case R.id.Btn1:

                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 1); //Gallery acessing
        }
    }

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

    if (resultCode == RESULT_OK) {
        super.setContentView(R.layout.activity_view);
        selectedImage = (ImageView) findViewById(R.id.selectedImage); //Onresult redarct to anothr activity
    }
    Uri photoUri = data.getData();
    if (photoUri != null) {
        try {
            currentImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
            selectedImage.setImageBitmap(currentImage);

        } catch (Exception e) {
            e.printStackTrace();

            Intent myIntent = new Intent(this, ViewActivity.class);
            startActivity(myIntent); //switch activity
             }
    }}

    }

3 个答案:

答案 0 :(得分:2)

private View.OnClickListener myClickListener = new View.OnClickListener(){
    public void onClick(View view){
        //do all the work here
    }
}

然后将其设置在您的视图中

view1.setOnClickListener(myClickListener);
view2.setOnClickListener(myClickListener);
//... etc

答案 1 :(得分:2)

您可以尝试以下代码。我创建了一个公共方法doSomething(),该方法在每次单击按钮时都会被调用,因此无论何时调用它,它都会执行相同的功能。

    public void onClick(View v) {
    switch (v.getId()) {

            case R.id.Btn1:
                doSomething();
                break;
            case R.id.Btn2:
                doSomething();
                break;
        }
    }

public void doSomething(){
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 1); //Gallery acessing
}


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

    if (resultCode == RESULT_OK) {
        super.setContentView(R.layout.activity_view);
        selectedImage = (ImageView) findViewById(R.id.selectedImage); //Onresult redarct to anothr activity
    }
    Uri photoUri = data.getData();
    if (photoUri != null) {
        try {
            currentImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
            selectedImage.setImageBitmap(currentImage);

        } catch (Exception e) {
            e.printStackTrace();

            Intent myIntent = new Intent(this, ViewActivity.class);
            startActivity(myIntent); //switch activity
             }
    }}

    }

答案 2 :(得分:0)

您可以在其他视图中使用第一个按钮的performClick单击。

Button button1 = findViewById(R.id.Btn1)
case R.id.Btn2:
 button1.performClick()