我有大约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);
}
});
答案 0 :(得分:2)
当我阅读代码时,使用相同的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);
}
};
现在将所有按钮链接到监听器
b1.setOnClickListener(BtnSubmitListener);
b2.setOnClickListener(BtnSubmitListener);
答案 1 :(得分:0)
使用公共监听器
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类定义以提供上下文)