我有 5 个 editTexts 和每个 editText 的 2 个按钮,所以有 10 个。每个都应该增加/减少特定的editText。现在我的代码完全可以工作,但正如您很快就会看到的那样,它效率不高,甚至不能确保应该包含分钟的 editTexts 的数字在 0-59 之间(可以设置为 n 分钟的时钟) . 无论如何,我只想知道正确执行此操作的最佳或最有效的方法。
我的想法是有一个单独的方法来接收参数,例如特定的 editText 来递增/递减,另一个参数是添加还是减去,但我不确定实现,因为 OnClickListener() 必须有 public void onClick(查看 v) 方法。
谢谢!
代码:
package com.clock;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class addCourse extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcourse);
final Button save_Button = (Button) findViewById(R.id.addSave);
final Button cancel_Button = (Button) findViewById(R.id.addCancel);
final Button higherNumber_Button = (Button) findViewById(R.id.higherNumber_Button);
final Button lowerNumber_Button = (Button) findViewById(R.id.lowerNumber_Button);
final Button higherClasshh_Button = (Button) findViewById(R.id.higherClasshh_Button);
final Button lowerClasshh_Button = (Button) findViewById(R.id.lowerClasshh_Button);
final Button higherClassmm_Button = (Button) findViewById(R.id.higherClassmm_Button);
final Button lowerClassmm_Button = (Button) findViewById(R.id.lowerClassmm_Button);
final Button higherClockhh_Button = (Button) findViewById(R.id.higherClockhh_Button);
final Button lowerClockhh_Button = (Button) findViewById(R.id.lowerClockhh_Button);
final Button higherClockmm_Button = (Button) findViewById(R.id.higherClockmm_Button);
final Button lowerClockmm_Button = (Button) findViewById(R.id.lowerClockmm_Button);
final EditText courseCredits_Edit = (EditText) findViewById(R.id.courseCredits_Edit);
final EditText hhClass_Edit = (EditText) findViewById(R.id.classHours_Edit);
final EditText mmClass_Edit = (EditText) findViewById(R.id.classMins_Edit);
final EditText hhClock_Edit = (EditText) findViewById(R.id.clockHours_Edit);
final EditText mmClock_Edit = (EditText) findViewById(R.id.clockMins_Edit);
higherNumber_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
int a = Integer.parseInt(courseCredits_Edit.getText().toString());
++a;
courseCredits_Edit.setText(a + "");
}
});
lowerNumber_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
int a = Integer.parseInt(courseCredits_Edit.getText().toString());
--a;
courseCredits_Edit.setText(a + "");
}
});
higherClasshh_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
int a = Integer.parseInt(hhClass_Edit.getText().toString());
++a;
hhClass_Edit.setText(a + "");
}
});
lowerClasshh_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
int a = Integer.parseInt(hhClass_Edit.getText().toString());
--a;
hhClass_Edit.setText(a + "");
}
});
higherClassmm_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
int a = Integer.parseInt(mmClass_Edit.getText().toString());
++a;
mmClass_Edit.setText(a + "");
}
});
lowerClassmm_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
int a = Integer.parseInt(mmClass_Edit.getText().toString());
--a;
mmClass_Edit.setText(a + "");
}
});
higherClockhh_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
int a = Integer.parseInt(hhClock_Edit.getText().toString());
++a;
hhClock_Edit.setText(a + "");
}
});
lowerClockhh_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
int a = Integer.parseInt(hhClock_Edit.getText().toString());
--a;
hhClock_Edit.setText(a + "");
}
});
higherClockmm_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
int a = Integer.parseInt(mmClock_Edit.getText().toString());
++a;
mmClock_Edit.setText(a + "");
}
});
lowerClockmm_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
int a = Integer.parseInt(mmClock_Edit.getText().toString());
--a;
mmClock_Edit.setText(a + "");
}
});
save_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
cancel_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
finish();
}
});
}
}`