我正在尝试为这两个数字构建一个带有两个TextView字段的计算器。我想出了如何使用" in-app"输入数字?数字键盘为顶部数字,操作数1 [textView](我知道使用EditText会更容易,但这是一个分配)。我无法切换到第二个textView,操作数2 [textView2]。
当我完成输入textView的编号后,我想切换到textView2(使用加号,减号,多键和/或div按钮)并继续输入数字,以便我可以用它进行计算。 / p>
这是我的应用程序的图像。请忽略星星,进度条和raido按钮,因为它们是作业的一部分而不是与计算器有关。
您对我如何做到这一点有什么建议吗?
Android代码
package com.example.tristan.assn2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
String operand1 = "";
String operand2 = "";
String oneS = "1";
String twoS = "2";
String threeS = "3";
String fourS = "4";
String fiveS = "5";
String sixS = "6";
String sevenS = "7";
String eightS = "8";
String nineS = "9";
String zeroS = "0";
String dotS = ".";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view)
{
Button clickedButton = (Button) view;
Button one = (Button) findViewById(R.id.button);
Button two = (Button) findViewById(R.id.button2);
Button three = (Button) findViewById(R.id.button3);
Button four = (Button) findViewById(R.id.button4);
Button five = (Button) findViewById(R.id.button5);
Button six = (Button) findViewById(R.id.button6);
Button seven = (Button) findViewById(R.id.button7);
Button eight = (Button) findViewById(R.id.button8);
Button nine = (Button) findViewById(R.id.button9);
Button zero = (Button) findViewById(R.id.button10);
Button dot = (Button) findViewById(R.id.button11);
Button clear = (Button) findViewById(R.id.button12);
Button plus = (Button) findViewById(R.id.button17);
Button minus = (Button) findViewById(R.id.button15);
Button mult = (Button) findViewById(R.id.button19);
Button div = (Button) findViewById(R.id.button21);
Button sr = (Button) findViewById(R.id.button16);
Button fac = (Button) findViewById(R.id.button20);
Button dd = (Button) findViewById(R.id.button22);
Button equal = (Button) findViewById(R.id.button23);
//------------------------------------------------------------------------------------------
TextView textView = (TextView) findViewById(R.id.textView);
TextView textView2 = (TextView) findViewById(R.id.textView2);
//OPERAND1
if(clickedButton == minus) {
operand1 = "-";
list1.add(operand1);
}
if (clickedButton == one) {
operand1 = oneS;
list1.add(operand1);
}
if (clickedButton == two) {
operand1 = twoS;
list1.add(operand1);
}
if (clickedButton == three) {
operand1 = threeS;
list1.add(operand1);
}
if (clickedButton == four) {
operand1 = fourS;
list1.add(operand1);
}
if (clickedButton == five) {
operand1 = fiveS;
list1.add(operand1);
}
if (clickedButton == six) {
operand1 = sixS;
list1.add(operand1);
}
if (clickedButton == seven) {
operand1 = sevenS;
list1.add(operand1);
}
if (clickedButton == eight) {
operand1 = eightS;
list1.add(operand1);
}
if (clickedButton == nine) {
operand1 = nineS;
list1.add(operand1);
}
if (clickedButton == zero) {
operand1 = zeroS;
list1.add(operand1);
}
if (clickedButton == dot) {
operand1 = dotS;
list1.add(operand1);
}
//Builds String from ArrayList
StringBuilder sb1 = new StringBuilder();
for (String s1 : list1) {
sb1.append(s1);
}
//STRING NUMBER
String output1 = sb1.toString();
textView.setText(output1);
//NEED TO CONVERT TO DOUBLE*****************
//IF plus, minus, mult, div is pressed, switch to Operand 2 and input numbers
if(clickedButton == plus || clickedButton == minus || clickedButton == mult || clickedButton == div) {
if (clickedButton == one) {
operand2 = oneS;
list2.add(operand2);
}
if (clickedButton == two) {
operand2 = twoS;
list2.add(operand2);
}
if (clickedButton == three) {
operand2 = threeS;
list2.add(operand2);
}
if (clickedButton == four) {
operand2 = fourS;
list2.add(operand2);
}
if (clickedButton == five) {
operand2 = fiveS;
list2.add(operand2);
}
if (clickedButton == six) {
operand2 = sixS;
list2.add(operand2);
}
if (clickedButton == seven) {
operand2 = sevenS;
list2.add(operand2);
}
if (clickedButton == eight) {
operand2 = eightS;
list2.add(operand2);
}
if (clickedButton == nine) {
operand2 = nineS;
list2.add(operand2);
}
if (clickedButton == zero) {
operand2 = zeroS;
list2.add(operand2);
}
if (clickedButton == dot) {
operand2 = dotS;
list2.add(operand2);
}
//Builds String from ArrayList
StringBuilder sb2 = new StringBuilder();
for (String s2 : list2) {
sb2.append(s2);
}
//STRING NUMBER
String output2 = sb2.toString();
textView2.setText(output2);
}
}
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tristan.assn2.MainActivity">
<Button
android:text="/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button21"
android:fontFamily="sans-serif-medium"
android:layout_alignBaseline="@+id/button18"
android:layout_alignBottom="@+id/button18"
android:layout_toRightOf="@+id/button18"
android:layout_alignRight="@+id/button15"
android:layout_alignEnd="@+id/button15"
android:onClick="sendMessage"/>
<Button
android:text="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="sendMessage"/>
<Button
android:text="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_toLeftOf="@+id/button3"
android:layout_toStartOf="@+id/button3"
android:onClick="sendMessage"/>
<Button
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginBottom="61dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/button2"
android:layout_toStartOf="@+id/button2"
android:onClick="sendMessage"/>
<Button
android:text="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button4"
android:layout_above="@+id/button2"
android:layout_alignLeft="@+id/button11"
android:layout_alignStart="@+id/button11"
android:onClick="sendMessage"/>
<Button
android:text="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_toLeftOf="@+id/button3"
android:layout_toStartOf="@+id/button3"
android:id="@+id/button5"
android:onClick="sendMessage"/>
<Button
android:text="6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button5"
android:layout_toRightOf="@+id/button2"
android:layout_toEndOf="@+id/button2"
android:id="@+id/button6"
android:onClick="sendMessage"/>
<Button
android:text="7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button7"
android:layout_above="@+id/button4"
android:layout_alignLeft="@+id/button4"
android:layout_alignStart="@+id/button4"
android:onClick="sendMessage"/>
<Button
android:text="8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button7"
android:layout_toRightOf="@+id/button4"
android:layout_toEndOf="@+id/button4"
android:id="@+id/button8"
android:onClick="sendMessage"/>
<Button
android:text="9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button6"
android:layout_toRightOf="@+id/button5"
android:id="@+id/button9"
android:layout_alignTop="@+id/button8"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="sendMessage"/>
<Button
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_toLeftOf="@+id/button3"
android:layout_toStartOf="@+id/button3"
android:id="@+id/button10"
android:onClick="sendMessage"/>
<Button
android:text="."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button10"
android:layout_toLeftOf="@+id/button2"
android:id="@+id/button11"
android:layout_below="@+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="87dp"
android:layout_marginStart="87dp"
android:layout_marginTop="47dp"
android:onClick="sendMessage"/>
<Button
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button15"
android:layout_alignTop="@+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="41dp"
android:layout_marginStart="41dp"
android:layout_toLeftOf="@+id/button4"
android:layout_toStartOf="@+id/button4"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:onClick="sendMessage"/>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button17"
android:layout_alignBaseline="@+id/button15"
android:layout_alignBottom="@+id/button15"
android:layout_toStartOf="@+id/button4"
android:layout_toLeftOf="@+id/button4"
android:layout_marginRight="47dp"
android:layout_marginEnd="47dp"
android:onClick="sendMessage" />
<Button
android:text="x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button18"
android:layout_below="@+id/button17"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button17"
android:layout_alignEnd="@+id/button17"
android:fontFamily="sans-serif-medium"
android:onClick="sendMessage"/>
<Button
android:text="x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button19"
android:layout_below="@+id/button17"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button17"
android:layout_alignEnd="@+id/button17"
android:fontFamily="sans-serif-medium"
android:onClick="sendMessage"/>
<Button
android:text="sr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button16"
android:fontFamily="sans-serif-medium"
android:layout_below="@+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/button20"
android:layout_toStartOf="@+id/button20"
android:onClick="sendMessage"/>
<Button
android:text="!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button20"
android:fontFamily="sans-serif-medium"
android:layout_below="@+id/button21"
android:layout_alignLeft="@+id/button15"
android:layout_alignStart="@+id/button15"
android:layout_alignRight="@+id/button21"
android:layout_alignEnd="@+id/button21"
android:onClick="sendMessage"/>
<Button
android:text="DD - DMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button11"
android:layout_toStartOf="@+id/button"
android:id="@+id/button22"
android:layout_alignRight="@+id/button20"
android:layout_alignEnd="@+id/button20"
android:onClick="sendMessage"/>
<TextView
android:hint="Operand 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/textView"
android:layout_toLeftOf="@+id/button5"
android:layout_toStartOf="@+id/button5" />
<TextView
android:hint="Operand 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="13dp"
android:id="@+id/textView2"
android:layout_toLeftOf="@+id/button8"
android:layout_toStartOf="@+id/button8" />
<TextView
android:hint="Results"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/textView3"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
android:isIndicator="false"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_alignBottom="@+id/ratingBar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp" />
<RadioButton
android:text="Use for Operand 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton"
android:layout_below="@+id/button23"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<RadioButton
android:text="Use for Operand 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioButton"
android:layout_alignLeft="@+id/radioButton"
android:layout_alignStart="@+id/radioButton"
android:id="@+id/radioButton2" />
<Button
android:text="="
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button23"
android:layout_alignTop="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="sendMessage"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_below="@+id/radioButton2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button12"
android:layout_alignBaseline="@+id/button10"
android:layout_alignBottom="@+id/button10"
android:layout_toRightOf="@+id/button8"
android:layout_toEndOf="@+id/button8"
android:onClick="sendMessage"/>
</RelativeLayout>
答案 0 :(得分:2)
在加/减/ ..按钮点击发生后,您必须听取按键点击。使用旗帜。单击加/减/ ..或textview2时将标志设置为1,并将textview更改为textview2,将list1更改为list2。单击textview1时反之亦然。
答案 1 :(得分:1)
你可以这样做:
public class YourActivityName extends AppCompatActivity {
TextView operand1, operand2,result,tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dd);
operand1 = (TextView) findViewById(R.id.textView);
operand2 = (TextView) findViewById(R.id.textView2);
result= (TextView) findViewById(R.id.textView3);
tv=operand1;
}
public void sendMessage(View v) {
if (v.getId() == R.id.button19 || v.getId() == R.id.button15 || v.getId() == R.id.button17 || v.getId() == R.id.button21 || v.getId() == R.id.button18) {
tv=operand2;
}
if(v.getId()==R.id.button7){
tv.setText(tv.getText()+"7");
}
if(v.getId()==R.id.button8){
tv.setText(tv.getText()+"8");
}
if(v.getId()==R.id.button23){
int op1=Integer.parseInt(operand1.getText().toString());
int op2=Integer.parseInt(operand2.getText().toString());
op2+=op1;
result.setText(op2+"");
tv=operand1;
operand1.setText("");
operand2.setText("");
}
}
}
这是代码,仅用于按下'='按钮并仅从按钮'7'和'8'获取输入,您需要和其他按钮和操作的功能..但它应该解决您的更改问题textview for input ...
你也可以用旗帜做。正如@ Code-Apprentice所建议的......
答案 2 :(得分:0)
每当用户单击操作员按钮时,您应该切换布尔标志。然后,当用户单击数字按钮时,请检查此标志以确定正在使用哪个操作数。
与此标志一起使用时,您还可以使用TextView currentTextView
这样的变量来指定当前使用的TextView。然后,只要用户单击操作员按钮,就将此变量分配给另一个TextView。此策略将使您只需要if
中的OnClickListener
个语句作为操作员按钮。对于数字按钮,if
中不需要任何OnClickListener
个语句。
P.S。请注意,每个数字按钮都已显示数字字符。您可以使用此事实删除点击侦听器中的几乎所有if...else
链。
p.p.s。您应该考虑为每个按钮编写不同的OnClickListener
s。使用前面提到的策略,您可以为所有数字按钮设置一个侦听器,但我仍然建议为每个操作员按钮创建一个不同的侦听器。