优化代码public class SecondActivity extends AppCompatActivity { private TextView textView9, textView11, textView13, textView15, textView17, textView19; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent intent = getIntent(); String num = intent.getStringExtra("num"); String name = intent.getStringExtra("name"); String score1 = intent.getStringExtra("score1"); String score2 = intent.getStringExtra("score2"); String score3 = intent.getStringExtra("score3"); double average = intent.getDoubleExtra("average", 0); textView9 = findViewById(R.id.textView9); textView11 = findViewById(R.id.textView11); textView13 = findViewById(R.id.textView13); textView15 = findViewById(R.id.textView15); textView17 = findViewById(R.id.textView17); textView19 = findViewById(R.id.textView19); textView9.setText(num); textView11.setText(name); textView13.setText(score1); textView15.setText(score2); textView17.setText(score3); textView19.setText(String.format("%.2f", average)); } }
时间: 2024-01-22 15:01:56 浏览: 117
这段代码已经非常简洁,不过还可以进一步优化:
1. 将 findViewById() 方法的调用放在 onStart() 方法中,避免在 onCreate() 方法中执行过多的操作;
2. 使用 ButterKnife 或者 ViewBinding 来代替 findViewById() 方法,可以进一步简化代码。
下面是使用 ButterKnife 优化后的代码:
```
public class SecondActivity extends AppCompatActivity {
@BindView(R.id.textView9)
TextView textView9;
@BindView(R.id.textView11)
TextView textView11;
@BindView(R.id.textView13)
TextView textView13;
@BindView(R.id.textView15)
TextView textView15;
@BindView(R.id.textView17)
TextView textView17;
@BindView(R.id.textView19)
TextView textView19;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ButterKnife.bind(this);
Intent intent = getIntent();
String num = intent.getStringExtra("num");
String name = intent.getStringExtra("name");
String score1 = intent.getStringExtra("score1");
String score2 = intent.getStringExtra("score2");
String score3 = intent.getStringExtra("score3");
double average = intent.getDoubleExtra("average", 0);
textView9.setText(num);
textView11.setText(name);
textView13.setText(score1);
textView15.setText(score2);
textView17.setText(score3);
textView19.setText(String.format("%.2f", average));
}
}
```
使用 ViewBinding 优化后的代码:
```
public class SecondActivity extends AppCompatActivity {
private ActivitySecondBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivitySecondBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Intent intent = getIntent();
String num = intent.getStringExtra("num");
String name = intent.getStringExtra("name");
String score1 = intent.getStringExtra("score1");
String score2 = intent.getStringExtra("score2");
String score3 = intent.getStringExtra("score3");
double average = intent.getDoubleExtra("average", 0);
binding.textView9.setText(num);
binding.textView11.setText(name);
binding.textView13.setText(score1);
binding.textView15.setText(score2);
binding.textView17.setText(score3);
binding.textView19.setText(String.format("%.2f", average));
}
}
```
使用 ButterKnife 或者 ViewBinding 可以使代码更加简洁易读。
阅读全文