- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以编写一些代码,这样我就可以将它们全部分组在单个代码中,而不是编写单独的 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);
最佳答案
您可以在 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();
}
}
关于java - 我的 Android 应用程序上有 34 个按钮。有什么方法可以避免为每个人编写 onclicklistener 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22268387/
当我单击图像时,它不会更改显示 TextView ,我哪里出错了? **List fields = new ArrayList(); for(int i = 0; i < 64; i+
我想做的是提供一个平台列表供用户选择。通过单击一个平台,它会将相应的 sql 表名称的名称放入变量中。但我不知道如何在我的提交按钮 clickListener 中使用该变量。这是我正在使用的代码部分。
我可以在我的 recyclerView holder 中实现它们 public class PagosPendientesViewHolder extends RecyclerView.ViewHol
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
当我想向 setOnClickListener 添加参数(例如按钮)时,我从 eclipse 警报中单击 View.OnClickListener() ,但 eclipse 类型 new OnClic
我看了http://developer.android.com/reference/android/view/package-summary.html并看到 View 类有一个名为“View.OnCl
我正在尝试 android 上的示例应用程序。我可以获得输出。 studentFormsActiviyt.java package com.example.android.accelerometerp
我有一个关于实现 OnClickListeners 以使用 ADT 进行开发的问题。我不确定哪种方法更有效,谁能告诉我每种方法的优缺点? class x extends Activity implem
我要问一个非常基本的问题,但我被困了很长时间。 在卡片 View 之后有一个回收 View ,每行有 2 个图像。现在我想在图像上创建点击监听器而不是 recycleview。 这个 Activity
我的理解是,当我创建一个监听点击的按钮对象时,我必须: 创建按钮对象 使用OnClickListner让它监听用户的点击 使用onClick在用户点击按钮后执行 Action 现在, setOnCli
我正在学习 android 开发的初学者教程,但我不明白这个错误。 错误在行“callButton.setOnClickListener(new OnClickListener() {” 是的,我知道
当使用下面的代码时,我不断收到错误,我知道还有另一种使用 onclick 函数的方法,android:onclick ...但我更喜欢这种“更清洁”的方式。 textview 的 id 名称是正确的,
尝试将 onClickListener 添加到我的 listView 中的项目时,我收到一条错误消息:“View 类型中的方法 setOnClickListener(View.OnClickListe
我有一个问题...我有不同的 TextView 和一个监听器。像这些: help_modul.setOnClickListener(this); help_timetable.setO
我创建了一个自定义的 AutocompleteAdapter,我需要允许用户选择某个对象(客户)。 我的 AutoCompleteAdapter 看起来像这样: package nl.raakict.
我有一个程序,它有一个用户可以编辑的文本框。当用户按下按钮时,会创建一个对话框,显示用户的文本和确认“是/否”选项。 public void setIP(View v){ //get the
这个问题在这里已经有了答案: NullPointerException accessing views in onCreate() (13 个回答) What is a NullPointerExce
我有一个自定义 View ,我想当我设置 onClickListener 时我总是会崩溃。 public class Holder extends View implements OnClickLis
我有一组复选框,它们都需要 OnClickListener 来查看它们何时被选中/取消选中。这是我所拥有的: CheckBox[] sens = new CheckBox[3]; for (int i
我有两个简单的不同类,其中一个类扩展了另一个类。我的第一个类是: public class MyObject {} 里面没有任何东西。 还有另一个: public class People exten
我是一名优秀的程序员,十分优秀!