点击按钮的Android计算器
我试图通过添加值txtA+txtB来计算名为lblAnswer的字段。我对安卓开发领域相当陌生,我想知道什么是最好的方法。我已经在GUI中添加了必要的编辑字段。我现在正在java文件中尝试并创建该方法。此方法已命名为doCalc。以下是我到目前为止的情况点击按钮的Android计算器,android,button,methods,android-edittext,calculated-field,Android,Button,Methods,Android Edittext,Calculated Field,我试图通过添加值txtA+txtB来计算名为lblAnswer的字段。我对安卓开发领域相当陌生,我想知道什么是最好的方法。我已经在GUI中添加了必要的编辑字段。我现在正在java文件中尝试并创建该方法。此方法已命名为doCalc。以下是我到目前为止的情况 public void doCalc() { lblAnswer = txtA + txtB; } 有人建议我添加更多的代码,这里是完整的代码。谢谢你的建议 这是Java文件 package com.example.wa
public void doCalc()
{
lblAnswer = txtA + txtB;
}
有人建议我添加更多的代码,这里是完整的代码。谢谢你的建议
这是Java文件
package com.example.wattsprofessional;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void doCalc()
{
lblAnswer = txtA + txtB;
Double.parseDouble(txtA.getText().toString());
lblAnswer.setText"t
}
这是xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="Write Here"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtA"
android:layout_below="@+id/txtA"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="Second Here"
android:inputType="numberDecimal" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/calculate"
android:onClick="doCalc"/>
<TextView
android:id="@+id/lblAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="TextView" />
</RelativeLayout>
为了从
EditText
获取双精度值,您需要使用Double.parseDouble(txtA.getText().toString())
。要设置文本,可以使用lblAnswer.setText(“文本”)
此外,从按钮调用它的最简单方法是在XML中设置它的android:onClick
属性,例如android:onClick=“doCalc”
编辑:您还需要创建对对象的引用。在onCreate()之前,放置:
然后在onCreate()中,需要初始化对象:
txtA = new (EditText)findViewById(R.Id.txtA);
txtB = new (EditText)findViewById(R.Id.txtB);
lblAnswer = new (TextView)findViewById(R.Id.lblAnswer);
您的代码缺少几个关键组件。检查您的代码,并检查我在下面准备的代码
package com.example.wattsprofessional;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText txtA, txtB;
private Button button1;
// ^ we have declared these as fields up here so that we can access them throughout the page, past all the curly brackets
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtA = (EditText) findViewById(R.id.txtA);
txtB = (EditText) findViewById(R.id.txtB);
button1 = (Button) findViewById(R.id.button1);
// ^ this is where we initialize these. You did the xml correctly, but you still need to hook the java to it.
// it allows us to use any names and locations we like not just same ones.
// basically you say what it is (Button) and then use the following method to look for the id that you wrote in the xml
initButton();
// i made this listener so we'd have time. this is the oncreate method and is called instantly.
// if we called doCalc here, we'd have no time to put numbers in.
}
private void initButton() {
button1.setOnClickListener(new OnClickListener() {
// this one performs an action when our button is clicked. it performs whatever is below
@Override
public void onClick(View v) {
String strA = txtA.getText().toString();
String strB = txtB.getText().toString();
// we get our strings from our editexts. i think you know how to do this well.
Double dblAnswer = doCalc(strA, strB);
// ^we pass them to our method, it does all the heavy lifting for us. and spits an answer for us.
TextView lblAnswer = (TextView) findViewById(R.id.lblAnswer);
// this is a local variable, as opposed to a field. i made so you know that you can do it like this - with the whole line here
// the disadvantage is that we can't do anything to it outside of this curly bracket. but there are performs gains.
// in general it's wasteful to use fields when you can suffice with local variable
String answer = String.valueOf(dblAnswer);
// we get our answer and turn it to a string.
lblAnswer.setText(answer);
// finally we set our result to the textView.
}
});
}
public double doCalc(String a, String b) {
// a and b are both variables. they refer to the stuff we put in
double dblA = Double.parseDouble(a);
double dblB = Double.parseDouble(b);
// we're gonna make both of these numbers so we can add them. right now they're just text.
return dblA + dblB;
// ^ this statement means that this method will spit a number out when it's done which we can use however.
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
你到底有什么问题?据我们所知,一切进展顺利。我不能给你一个最好的方法,但我要做的是把我所有的计算都用不同的方法进行,这些方法取参数,然后返回一个值。谢谢你的快速回复。当我看到上面的实际方法时,lblAnswer=txtA+txtB;下面有一条错误线。它表示“无法解析为变量”。这是所有三个变量的错误消息。我不确定我把事情搞砸了。你需要将它们声明为全局变量,然后在
onCreate()
中初始化它们。也许你最好提供更多的代码。正如Dare所说,您是否将这些值称为ints
或double
或您希望使用的任何格式?@Dare。请阅读并理解什么是全局变量。这不是你的建议,谢谢你。我现在就去试试这个。为了支持您在android上所说的:onClick。我在GUI中进行了这项工作,这是正确的位置吗?是的,android:onClick
位于GUI的XML文件中。好的,所以我对上述建议有点困惑。我要做的是创建一个计算字段。txtA+txtB将是LBLANWER的计算字段?是。最简单的方法是创建变量来保存EditText中解析的值,然后将文本设置为Double.toString(num1+num2)
(假设num1和num2是您的变量)。谢谢。我很抱歉,但我仍在学习,甚至这对我来说有点困惑。我已经在修订版中提供了我的完整代码。我将尝试完成此代码。但我仍然不太确定如何处理这个问题。我知道我应该做什么,但不知道如何做,如果这样做,因为。谢谢你到目前为止最好的答复。我同意我有很多教程需要复习。这些细节帮助很大。
package com.example.wattsprofessional;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText txtA, txtB;
private Button button1;
// ^ we have declared these as fields up here so that we can access them throughout the page, past all the curly brackets
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtA = (EditText) findViewById(R.id.txtA);
txtB = (EditText) findViewById(R.id.txtB);
button1 = (Button) findViewById(R.id.button1);
// ^ this is where we initialize these. You did the xml correctly, but you still need to hook the java to it.
// it allows us to use any names and locations we like not just same ones.
// basically you say what it is (Button) and then use the following method to look for the id that you wrote in the xml
initButton();
// i made this listener so we'd have time. this is the oncreate method and is called instantly.
// if we called doCalc here, we'd have no time to put numbers in.
}
private void initButton() {
button1.setOnClickListener(new OnClickListener() {
// this one performs an action when our button is clicked. it performs whatever is below
@Override
public void onClick(View v) {
String strA = txtA.getText().toString();
String strB = txtB.getText().toString();
// we get our strings from our editexts. i think you know how to do this well.
Double dblAnswer = doCalc(strA, strB);
// ^we pass them to our method, it does all the heavy lifting for us. and spits an answer for us.
TextView lblAnswer = (TextView) findViewById(R.id.lblAnswer);
// this is a local variable, as opposed to a field. i made so you know that you can do it like this - with the whole line here
// the disadvantage is that we can't do anything to it outside of this curly bracket. but there are performs gains.
// in general it's wasteful to use fields when you can suffice with local variable
String answer = String.valueOf(dblAnswer);
// we get our answer and turn it to a string.
lblAnswer.setText(answer);
// finally we set our result to the textView.
}
});
}
public double doCalc(String a, String b) {
// a and b are both variables. they refer to the stuff we put in
double dblA = Double.parseDouble(a);
double dblB = Double.parseDouble(b);
// we're gonna make both of these numbers so we can add them. right now they're just text.
return dblA + dblB;
// ^ this statement means that this method will spit a number out when it's done which we can use however.
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}