简体   繁体   English

如何缩短代码

[英]How to shorten code

I am making a calculator and below is my code. 我在做一个计算器,下面是我的代码。 I was wondering is there any way to shorten the code, I have 18 buttons and I have to write 50 lines of code just to take reference from XML and add click listener to it 我想知道有什么方法可以缩短代码,我有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);

}

Is there any other way to write this code in shorter way? 还有其他以较短的方式编写此代码的方法吗?

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); 
}

There's no need for storing reference to each button as class members. 无需将对每个按钮的引用存储为类成员。 In the onClick listener we can determine which button was clicked. onClick侦听器中,我们可以确定单击了哪个按钮。

public onClick(View v){
    int number = Arrays.asList(ids).indexOf(v.getId()) + 1;
    // Button 'number' was clicked
}

If you add a method to do the findViewById and setOnClickListener , you can reduce the lines for each button from two to one: 如果添加方法来执行findViewByIdsetOnClickListener ,则可以将每个按钮的行从两行减少到一行:

private Button findAndSetClickListener(int id) {
  Button button = (Button) findViewById(id);
  button.setOnClickListener(this);
  return button;
}

Then: 然后:

b1 = findAndSetClickListener(R.id.b1);
// etc.

try this type code. 试试这种类型的代码。

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);




}

You could write an array where you store the views. 您可以编写一个数组来存储视图。

So that you can do something like 这样你就可以做

for (View v : array.getView()) {
    v.setOnClickListener(this)
}

I think the actual findViewById would be a little more complicated to simplify 我认为实际的findViewById要简化一点

You could shorten it at about 50% (and make it a lot more readable) with a private method: 您可以使用私有方法将其缩短50%左右(并使它更具可读性):

private Button getButtonWithListener(int id) {
    Button btn = (Button) findViewById(id);
    btn.setOnClickListener(this);
    return btn;
}

and call this at every button: (saves you the setOnClickListener) 并在每个按钮处调用它:(为您节省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);

This way you're not losing the reference to the button. 这样,您就不会丢失对该按钮的引用。 If you need those references to the buttons it won't become much shorter. 如果您需要对按钮的那些引用,它不会变得更短。 If you don't need them, then there are some excellent possibilities in the other answers here! 如果您不需要它们,那么这里的其他答案中就有一些极好的可能性!

If you need to shorten the button code, Use View's in OnClick() with help of switch statement. 如果需要缩短按钮代码,请在switch语句的帮助下使用OnClick()中的View。

In your XML Layout: 在您的XML布局中:

Create the number of buttons that you want with different button id. 使用不同的按钮ID创建所需的按钮数量。 But use same method name for all button's onClick attribute. 但是对所有按钮的onClick属性使用相同的方法名称。 Eg: android:onClick="submitBTN" used for all buttons. 例如: android:onClick =“ submitBTN”用于所有按钮。

In your MainActivity: 在您的MainActivity中:

Implement that method name to perform different operations using switch statement 使用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
    }
}

Try using butter knife library. 尝试使用黄油刀库。 You can use syntax like: 您可以使用如下语法:

@Onclick({R.id.b1, R.id.b2...})
public void handleClick(Button  btn){
         // handle click event here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM