所以我试着制作一个计算器(第一个应用程序),但不是单独设置每个按钮,是否可以立即完成所有这些操作?
这就是它现在的设置方式:
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (decimal == true) {
String onScr = String.valueOf(firstTextView.getText());
onScr = onScr + 1;
firstTextView.setText(onScr);
CurrentVal+= 0.1;
}
else {
if (CurrentVal == 0) {
firstTextView.setText("1");
CurrentVal++;
} else {
String onScr = String.valueOf(firstTextView.getText());
onScr = onScr + 1;
firstTextView.setText(onScr);
}
}
}
});
我设法让它工作,但想要压缩代码。
答案 0 :(得分:0)
Such as multiple button click listners...
Button btminus = (ImageButton)findViewById(R.id.btminus);
btminus.setOnClickListener(this);
Button btplus = (ImageButton)findViewById(R.id.btplus);
btplus.setOnClickListener(this);
那么你的活动想要实现onclicklistener,你得到的实现方法是:
public void onClick(View v) {
switch (v.getId()) {
case R.id.btplus:
Toast.makeText(getApplicationContext(), "Plus is clicked" + "+", Toast.LENGTH_SHORT).show();
break;
case R.id.btminus:
Toast.makeText(getApplicationContext(),"Minus is clicked" + "-", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
答案 1 :(得分:0)
您可以在implement View.OnClickListener
(或Activity
)中Fragment
,然后在设置Listener
时将其作为Buttons
传递。
这样您就可以压缩点击处理代码并清理按钮分配部分。更简洁的代码,更易于维护。
public class MyActivity extends Activity implements View.OnClickListener
{
private Button button1;
private Button button2;
@Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.your_layout);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
// since your Activity implements View.OnClickListener
// you can pass it as an argument here.
button1.setOnClickListener(this)
button2.setOnClickListener(this)
}
@Override
public void onClick(View v)
{
// get the clicked view's Id and act accordingly
switch (v.getId())
{
case R.id.button1:
clickedButton1();
break;
case R.id.button2:
clickedButton2();
break;
// if you didn't set up a 'case' for a received click, log it
default:
Log.e(TAG, "Unhandled button press!");
break;
}
}
// if your code is moderately long you can
// move it outside of your onClick() too
private void clickedButton1()
{
// do something
}
private void clickedButton2()
{
// do something else
}
}
答案 2 :(得分:0)
public class MainActivity extends Activity implements OnClickListener {
Button Btn1, Btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Btn1 = (Button) findViewById(R.id.btn1);
Btn2 = (Button) findViewById(R.id.btn2);
Btn1.setOnClickListener(this);
Btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
Log.i("myTag", "Button1 Clicked");
break;
case R.id.btn2:
Log.i("myTag", "Button2 Clicked");
break;
}
}
答案 3 :(得分:0)
这就是我的方式。这不完全是您要求的,但您可以根据自己的需要进行更改。它适用于数字按钮。您必须为其他按钮编写更多onClickListeners。这里, resultString 是一个EditText。
private ArrayList<Integer> calculatorButtonList;
calculatorButtonList = new ArrayList<Integer>();
calculatorButtonList.add(R.id.button0);
calculatorButtonList.add(R.id.button1);
calculatorButtonList.add(R.id.button2);
calculatorButtonList.add(R.id.button3);
calculatorButtonList.add(R.id.button4);
calculatorButtonList.add(R.id.button5);
calculatorButtonList.add(R.id.button6);
calculatorButtonList.add(R.id.button7);
calculatorButtonList.add(R.id.button8);
calculatorButtonList.add(R.id.button9);
for(int i=0; i<calculatorButtonList.size(); i++) {
final String buttonText = ((Button) findViewById(calculatorButtonList.get(i))).getText().toString();
((Button) findViewById(calculatorButtonList.get(i))).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
resultString.setText(resultString.getText().append(buttonText));
resultString.setSelection(pairingKey.getText().length());
}
});
}