如何有效地处理许多按钮的重复代码

How to deal with repeated code for many buttons in an efficient way

我有大约20个按钮b1,b2,b3,...bn,内容几乎相同,我不想重复相同的代码,我想要的是:一个高效的代码,以免重复相同的代码,这是我的代码:

b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Changes per button
            act1 = titl1;
            act2= tex1;
            act3=pic1;

            //Common code for all buttons
            Bundle b1 = new Bundle();
            Bundle b2 = new Bundle();
            Bundle b3 = new Bundle();
            b1.putString("somekey1", act1);
            b2.putString("somekey2", act2);
            b3.putString("somekey2", act3);
            Intent i = new Intent(getApplicationContext(), Content1.class);
            i.putExtras(b1);
            i.putExtras(b2);
            i.putExtras(b3);
            startActivity(i);
            overridePendingTransition(R.anim.pushinhorizontal,
                    R.anim.pushouthorizontal);
        }
    });

    b2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Changes per button
            act1 = titl2;
            act2= tex2;
            act3=pic2;

            //Common code for all buttons
            Bundle b1 = new Bundle();
            Bundle b2 = new Bundle();
            Bundle b3 = new Bundle();
            b1.putString("somekey1", act1);
            b2.putString("somekey2", act2);
            b3.putString("somekey2", act3);
            Intent i = new Intent(getApplicationContext(), Content1.class);
            i.putExtras(b1);
            i.putExtras(b2);
            i.putExtras(b3);
            startActivity(i);
            overridePendingTransition(R.anim.pushinhorizontal,
                    R.anim.pushouthorizontal);
        }
    });

正如我阅读代码一样使用OOP概念使代码成为一个函数并调用它

样式 1

b1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) { 
            methodA(titl1, text1, pic1);

    }
});

b2.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) { 
            methodA(titl2, text2, pic2);

    }
});

private void methodA(object act1, object act2, object act3)
{
        Bundle b1 = new Bundle();
        Bundle b2 = new Bundle();
        Bundle b3 = new Bundle();
        b1.putString("somekey1", act1);
        b2.putString("somekey2", act2);
        b3.putString("somekey2", act3);
        Intent i = new Intent(getApplicationContext(), Content1.class);
        i.putExtras(b1);
        i.putExtras(b2);
        i.putExtras(b3);
        startActivity(i);
        overridePendingTransition(R.anim.pushinhorizontal,
                R.anim.pushouthorizontal);
}

现在您需要在需要重复相同代码时调用 methodA()

样式 2

private OnClickListener BtnSubmitListener = new OnClickListener() {
    public void onClick(View v) {

      act1 = titl2;
      act2= tex2;
      act3=pic2;

      Bundle b1 = new Bundle();
      Bundle b2 = new Bundle();
      Bundle b3 = new Bundle();
      b1.putString("somekey1", act1);
      b2.putString("somekey2", act2);
      b3.putString("somekey2", act3);
      Intent i = new Intent(getApplicationContext(), Content1.class);
      i.putExtras(b1);
      i.putExtras(b2);
      i.putExtras(b3);
      startActivity(i);
      overridePendingTransition(R.anim.pushinhorizontal,
            R.anim.pushouthorizontal);
    }

};

现在 link 您所有的按钮都指向监听器

b1.setOnClickListener(BtnSubmitListener);
b2.setOnClickListener(BtnSubmitListener);

使用通用监听器

public class YourActivity extends Activity { //or Fragment
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) { //or wherever you are currently setting your button listeners
        super.onCreate(savedInstanceState)
        ...
        Button b1 = (Button) findViewById(R.id./*button1 Id*/);
        ...
        Button bn = (Button) findViewById(R.id./*button2 Id*/);
        ...
        //set the common listener for all the buttons
        b1.setOnClickListener(mButtonListener);
        ...
        bn.setOnClickListener(mButtonListener);
        ...
    }
    ...
    //common listener
    private View.OnClickListener mButtonListener = new View.OnClickListener() {
        @Override
        public void OnClick(View v) {
            //do common code here
            switch (v.getId) {
                case R.id./*buttonid*/ :
                    //do button specific stuff here
                ...
            }
        }  
    };
}

编辑:(用 Activity class 定义包围早期代码以提供上下文)