重写代码以提高效率(按钮增量文本视图)
我有 5 个 editText,每个 editText 有 2 个按钮,所以有 10 个。每个都应该增加/减少特定的 editText。现在我的代码完全可以工作,但正如您很快就会看到的,它效率不高,甚至不能确保应该包含分钟的 editText 的数字在 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();
}
});
}
}`
I have 5 editTexts and 2 buttons for each editText so 10 of them. Each one is supposed to increment/decrement a specific editText. Now my code works entirely but as you'll soon see, it is not efficient and doesn't even make sure the numbers are between 0-59 (clock that can be set for n minutes) for the editTexts that are supposed to contain minutes. Anyhow I just want to know the best or most-efficient way to properly do this.
My idea was to have a separate method that takes in parameters such as the specific editText to increment/decrement and another param for whether to add or subtract but I am not sure for the implementation since OnClickListener() has to have the public void onClick(View v) method.
Thanks!
Code:
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();
}
});
}
}`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1) 在 xml 布局中使用每个按钮的“tag”属性。
http://developer.android.com/reference/android/view/View.html
xml中也使用onclick
2)在代码中的
: http://developer .android.com/reference/android/view/View.html#getTag()
如果你想以编程方式,使用上面的代码,获取相应的编辑文本,只需将它们的 ID 存储在数组列表中并使用 int即可得到对应的编辑文本。
1) use the "tag" property of each one of your button in your xml layout.
http://developer.android.com/reference/android/view/View.html
2) use also onclick in your xml
in your code:
http://developer.android.com/reference/android/view/View.html#getTag()
If you want to programmaticaly, with the code above, get the corresponding edittext, just store their ID in an arraylist and play with the int to get the corresponding EditText.