问题描述
我可以编写一些代码,以便代替编写单独的onClick侦听器而将它们全部组合在一个代码段中吗? 目前我有这个。 现在如何避免为每个方法编写onclicklistener方法。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DecimalFormat currencyFormatter = (DecimalFormat) NumberFormat.getInstance();
char decimalSeparator = currencyFormatter.getDecimalFormatSymbols().getDecimalSeparator();
mDecimalSeparator = Character.toString(decimalSeparator);
setContentView(R.layout.main);
mInputStack = new Stack<String>();
mOperationStack = new Stack<String>();
inputText = (TextView) findViewById(R.id.InputText);
inputText.setText("0");
resultText = (TextView) findViewById(R.id.ResultText);
resultText.setText("");
One = (Button) findViewById(R.id.button1);
Two = (Button) findViewById(R.id.button2);
Three = (Button) findViewById(R.id.button3);
Four = (Button) findViewById(R.id.button4);
Five = (Button) findViewById(R.id.button5);
Six = (Button) findViewById(R.id.button6);
Seven = (Button) findViewById(R.id.button7);
Eight = (Button) findViewById(R.id.button8);
Nine = (Button) findViewById(R.id.button9);
Zero = (Button) findViewById(R.id.button0);
Add = (Button) findViewById(R.id.buttonAdd);
Subtract = (Button) findViewById(R.id.buttonSubtract);
Multiply = (Button) findViewById(R.id.buttonMultiply);
Divide = (Button) findViewById(R.id.buttonDivide);
Delete = (Button) findViewById(R.id.buttonDel);
Period = (Button) findViewById(R.id.buttonPeriod);
Equal = (Button) findViewById(R.id.buttonEqual);
Sin = (Button) findViewById(R.id.buttonSin);
Cos = (Button) findViewById(R.id.buttonCos);
Tan = (Button) findViewById(R.id.buttonTan);
Cosec = (Button) findViewById(R.id.buttonCosec);
Sec = (Button) findViewById(R.id.buttonSec);
Cot = (Button) findViewById(R.id.buttonCot);
Sinh = (Button) findViewById(R.id.buttonSinh);
Cosh = (Button) findViewById(R.id.buttonCosh);
Tanh = (Button) findViewById(R.id.buttonTanh);
Factorial = (Button) findViewById(R.id.buttonFactorial);
Log = (Button) findViewById(R.id.buttonLog10);
Ln = (Button) findViewById(R.id.buttonLoge);
Reciprocal = (Button) findViewById(R.id.buttonReciprocal);
Square = (Button) findViewById(R.id.buttonSquare);
SquareRoot = (Button) findViewById(R.id.buttonSquareRoot);
Power = (Button) findViewById(R.id.buttonPower);
Percent = (Button) findViewById(R.id.buttonPercent);
One.setOnClickListener(this);
Two.setOnClickListener(this);
Three.setOnClickListener(this);
Four.setOnClickListener(this);
Five.setOnClickListener(this);
Six.setOnClickListener(this);
Seven.setOnClickListener(this);
Eight.setOnClickListener(this);
Nine.setOnClickListener(this);
Zero.setOnClickListener(this);
Add.setOnClickListener(this);
Subtract.setOnClickListener(this);
Divide.setOnClickListener(this);
Multiply.setOnClickListener(this);
Equal.setOnClickListener(this);
Delete.setOnClickListener(this);
Period.setOnClickListener(this);
Sin.setOnClickListener(this);
Cos.setOnClickListener(this);
Tan.setOnClickListener(this);
Cot.setOnClickListener(this);
Sec.setOnClickListener(this);
Cosec.setOnClickListener(this);
Sinh.setOnClickListener(this);
Cosh.setOnClickListener(this);
Tanh.setOnClickListener(this);
Percent.setOnClickListener(this);
Reciprocal.setOnClickListener(this);
Log.setOnClickListener(this);
Ln.setOnClickListener(this);
Power.setOnClickListener(this);
Square.setOnClickListener(this);
SquareRoot.setOnClickListener(this);
Factorial.setOnClickListener(this);
Sign.setOnClickListener(this);
1楼
您可以在xml中设置android:click属性。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="Go!"
android:onClick="goButtonClicked" android:id="@+id/goButton"></Button>
</LinearLayout>
SimplestButtonActivity.java
package com.botbook.simplestbutton;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class SimplestButtonActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void goButtonClicked(View v) {
tToast("Go button clicked!");
}
private void tToast(String s) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, s, duration);
toast.show();
}
}
2楼
使用开关盒onClick方法,您可以在其中使用不同按钮的ID触发事件。
喜欢,
Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
break;
case R.id.button2:
break;
case R.id.button3:
break;
//....add here add buttons
default:
break;
}
}
或者您可以执行与
onClick(View v)
对于不同的Button,并在XML文件中为Button设置,
android:onClick="onClick"
其他按钮也一样,您可以调用不同的方法。
3楼
您可以在xml中设置android:click属性。 这将允许您决定在xml中单击按钮时要调用的函数。 该函数必须具有与常规onClick函数相同的参数,并且必须是您的活动的一部分,但可以命名为任何您想要的名称-如onePressed,twoPressed等。
4楼
尝试这个
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
// do stuff;
break;
case R.id.button2:
// do stuff;
break;
case R.id.button3:
// do stuff;
break;
//....add here add buttons
default:
break;
}
}