gpt4 book ai didi

java - Android - 如何保持按钮被单击(选定)?

转载 作者:行者123 更新时间:2023-12-01 20:53:22 29 4
gpt4 key购买 nike

我正在尝试构建一个计算器,其中包含两个数字的两个 TextView 字段。我弄清楚了如何使用“应用程序内”数字键盘输入顶部数字 Operand 1 [textView] 的数字(我知道使用 EditText 会更容易,但这是为了作业)。我在切换到第二个 textView、操作数 2 [textView2] 时遇到问题。

当我输入完 textView 的数字后,我想切换到 textView2(使用加号、减号、乘数和/或 div 按钮)并继续输入数字,以便我可以使用它进行计算。

这是我的应用程序的外观图像。请忽略星星、进度条和raido按钮,因为它们是作业的一部分,但与计算器无关。 enter image description here

您对我如何做到这一点有什么建议吗?

Android Code

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 File

<?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>

最佳答案

您必须在加号/减号/..按钮单击发生后监听按键单击声。使用旗帜。当单击 plus/minus/.. 或 textview2 时设置标志 1,并将 textview 更改为 textview2,将 list1 更改为 list2。单击 textview1 时反之亦然。

关于java - Android - 如何保持按钮被单击(选定)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42825131/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com