[英]How to shorten code
我在做一個計算器,下面是我的代碼。 我想知道有什么方法可以縮短代碼,我有18個按鈕,我必須編寫50行代碼,以便從XML中獲取引用並為其添加點擊偵聽器
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String SelectedOpertator;
int num1, num2, result;
EditText input;
Button b1, b2, b3, b4, b5, b6 ,b7, b8, b9, b0, bdot;
Button bc, bs, bd, bp, bmi, bm, be;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
b5 = (Button) findViewById(R.id.b5);
b6 = (Button) findViewById(R.id.b6);
b7 = (Button) findViewById(R.id.b7);
b8 = (Button) findViewById(R.id.b8);
b9 = (Button) findViewById(R.id.b9);
b0 = (Button) findViewById(R.id.b0);
bdot = (Button) findViewById(R.id.bdot);
bc = (Button) findViewById(R.id.bc);
bs = (Button) findViewById(R.id.bs);
bd = (Button) findViewById(R.id.bd);
bp = (Button) findViewById(R.id.bp);
bmi = (Button) findViewById(R.id.bmi);
bm = (Button) findViewById(R.id.bm);
be = (Button) findViewById(R.id.be);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b0.setOnClickListener(this);
bdot.setOnClickListener(this);
bc.setOnClickListener(this);
bs.setOnClickListener(this);
bd.setOnClickListener(this);
bp.setOnClickListener(this);
bmi.setOnClickListener(this);
bm.setOnClickListener(this);
be.setOnClickListener(this);
}
還有其他以較短的方式編寫此代碼的方法嗎?
int ids[] = new int[] {R.id.b1, R.id.b2, R.id.b3, R.id.b4, R.id.b5, R.id.b6, R.id.b7, R.id.b8, R.id.b9, R.id.b0}
for(int i = 0; i < ids.length, i += 1){
findViewById(ids[i]).setOnClickListener(this);
}
無需將對每個按鈕的引用存儲為類成員。 在onClick
偵聽器中,我們可以確定單擊了哪個按鈕。
public onClick(View v){
int number = Arrays.asList(ids).indexOf(v.getId()) + 1;
// Button 'number' was clicked
}
如果添加方法來執行findViewById
和setOnClickListener
,則可以將每個按鈕的行從兩行減少到一行:
private Button findAndSetClickListener(int id) {
Button button = (Button) findViewById(id);
button.setOnClickListener(this);
return button;
}
然后:
b1 = findAndSetClickListener(R.id.b1);
// etc.
試試這種類型的代碼。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String SelectedOpertator;
int num1, num2, result;
EditText input;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
((Button) findViewById(R.id.b1)).setOnClickListener(this);
((Button) findViewById(R.id.b2)).setOnClickListener(this);
((Button) findViewById(R.id.b3)).setOnClickListener(this);
((Button) findViewById(R.id.b4)).setOnClickListener(this);
((Button) findViewById(R.id.b5)).setOnClickListener(this);
((Button) findViewById(R.id.b6)).setOnClickListener(this);
((Button) findViewById(R.id.b7)).setOnClickListener(this);
((Button) findViewById(R.id.b8)).setOnClickListener(this);
((Button) findViewById(R.id.b9)).setOnClickListener(this);
((Button) findViewById(R.id.b0)).setOnClickListener(this);
((Button) findViewById(R.id.bdot)).setOnClickListener(this);
((Button) findViewById(R.id.bc)).setOnClickListener(this);
((Button) findViewById(R.id.bs)).setOnClickListener(this);
((Button) findViewById(R.id.bp)).setOnClickListener(this);
((Button) findViewById(R.id.bmi)).setOnClickListener(this);
((Button) findViewById(R.id.bm)).setOnClickListener(this);
((Button) findViewById(R.id.be)).setOnClickListener(this);
}
您可以編寫一個數組來存儲視圖。
這樣你就可以做
for (View v : array.getView()) {
v.setOnClickListener(this)
}
我認為實際的findViewById
要簡化一點
您可以使用私有方法將其縮短50%左右(並使它更具可讀性):
private Button getButtonWithListener(int id) {
Button btn = (Button) findViewById(id);
btn.setOnClickListener(this);
return btn;
}
並在每個按鈕處調用它:(為您節省setOnClickListener)
b1 = getButtonWithListener(R.id.b1);
b2 = getButtonWithListener(R.id.b2);
b3 = getButtonWithListener(R.id.b3);
b4 = getButtonWithListener(R.id.b4);
b5 = getButtonWithListener(R.id.b5);
b6 = getButtonWithListener(R.id.b6);
b7 = getButtonWithListener(R.id.b7);
b8 = getButtonWithListener(R.id.b8);
b9 = getButtonWithListener(R.id.b9);
b0 = getButtonWithListener(R.id.b0);
bdot = getButtonWithListener(R.id.bdot);
bc = getButtonWithListener(R.id.bc);
bs = getButtonWithListener(R.id.bs);
bd = getButtonWithListener(R.id.bd);
bp = getButtonWithListener(R.id.bp);
bmi = getButtonWithListener(R.id.bmi);
bm = getButtonWithListener(R.id.bm);
be = getButtonWithListener(R.id.be);
這樣,您就不會丟失對該按鈕的引用。 如果您需要對按鈕的那些引用,它不會變得更短。 如果您不需要它們,那么這里的其他答案中就有一些極好的可能性!
如果需要縮短按鈕代碼,請在switch語句的幫助下使用OnClick()中的View。
在您的XML布局中:
使用不同的按鈕ID創建所需的按鈕數量。 但是對所有按鈕的onClick屬性使用相同的方法名稱。 例如: android:onClick =“ submitBTN”用於所有按鈕。
在您的MainActivity中:
使用switch語句實現該方法名稱以執行不同的操作
public void submitBTN(View view) {
switch (view.getId()) {
case R.id.btnClick1: // Code of button 1 Click
break;
case R.id.btnClick2: // Code of button 2 Click
break;
case R.id.btnClick3: // Code of button 3 Click
break;
case R.id.btnClickn: // Code of Button n Click
}
}
嘗試使用黃油刀庫。 您可以使用如下語法:
@Onclick({R.id.b1, R.id.b2...})
public void handleClick(Button btn){
// handle click event here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.