Java 计算器应用程序问题-开始为0,数字为点
我一直在开发android studio计算器。我是个初学者,我有两个问题。首先,当我在我的应用程序中按“c”键时,它在第一次点击时就可以正常工作。然后,当我点击它并点击任何数字时,它会显示“0x”(x就是那个数字) 我的第二个问题是如何只添加一个点?我尝试了String.contains(),但没有成功。我怎样才能解决这个问题? 以下是我的XML:Java 计算器应用程序问题-开始为0,数字为点,java,android,calculator,Java,Android,Calculator,我一直在开发android studio计算器。我是个初学者,我有两个问题。首先,当我在我的应用程序中按“c”键时,它在第一次点击时就可以正常工作。然后,当我点击它并点击任何数字时,它会显示“0x”(x就是那个数字) 我的第二个问题是如何只添加一个点?我尝试了String.contains(),但没有成功。我怎样才能解决这个问题? 以下是我的XML: <android.support.constraint.ConstraintLayout xmlns:android="http://sc
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000"
android:orientation="vertical"
android:weightSum="6"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:inputType="none"
android:padding="10dp"
android:text="0"
android:textAlignment="viewEnd"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="32sp"
android:textStyle="normal"
android:ellipsize="end"
android:maxLines="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/cc"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C"
android:textColor="#d75700"
android:textSize="30dp"
android:onClick="nrClear"
/>
<Button
android:id="@+id/back"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="undo"
android:textSize="20dp"
android:onClick="goBack"/>
<Button
android:id="@+id/division"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="/"
android:textSize="30dp"
android:onClick="pressDivision"/>
<Button
android:id="@+id/multip"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="*"
android:textSize="30dp"
android:onClick="pressMultiplication"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/nr1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:textSize="30dp"
android:onClick="firstNumber"/>
<Button
android:id="@+id/nr2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:textSize="30dp"
android:onClick="secondNumber"/>
<Button
android:id="@+id/nr3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"
android:textSize="30dp"
android:onClick="thirdNumber"/>
<Button
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"
android:textSize="30dp"
android:onClick="pressMinus"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/nr4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:textSize="30dp"
android:onClick="fourthNumber"/>
<Button
android:id="@+id/nr5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"
android:textSize="30dp"
android:onClick="fifthNumber"/>
<Button
android:id="@+id/nr6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"
android:textSize="30dp"
android:onClick="sixthNumber"/>
<Button
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"
android:textSize="30dp"
android:onClick="pressPlus"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="0dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/nr7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"
android:textSize="30dp"
android:onClick="seventhNumber"/>
<Button
android:id="@+id/nr8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"
android:textSize="30dp"
android:onClick="eightNumber"/>
<Button
android:id="@+id/nr9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9"
android:textSize="30dp"
android:onClick="ninthNumber"/>
<Button
android:id="@+id/equall"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="="
android:textSize="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="0dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="4"
android:textSize="30dp">
<Button
android:id="@+id/procent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="%"
android:textSize="30dp"
android:onClick="pressProcent"/>
<Button
android:id="@+id/nr0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"
android:textSize="30dp"
android:onClick="pressZero"
/>
<Button
android:id="@+id/dot"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="."
android:textSize="30dp"
android:onClick="pressDot"/>
<Button
android:id="@+id/equal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="="
android:textSize="30dp"
android:onClick="pressEquals"/>
</LinearLayout>
</LinearLayout>
和代码:
String operation;
CharSequence num1;
CharSequence num2;
TextView t;
String n1;
String n2;
String doot;
double number1;
double number2;
double output;
String out;
CharSequence temp;
CharSequence dooot;
int x;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
operation = "";
num2 = "";
n1 = "";
n2 = "";
doot = "";
number1 = 1;
number2 = 0;
output = 0;
out = "";
temp = "";
x=100;
dooot=".";
}
public void firstNumber(View view) {
TextView t = (TextView) findViewById(R.id.textView);
temp = t.getText();
if (temp.equals("0")) {
t.setText("1");
} else t.append("1");
}
public void secondNumber(View view) {
TextView t = (TextView) findViewById(R.id.textView);
temp = t.getText();
if (temp.equals("0")) {
t.setText("2");
} else t.append("2");
}
public void thirdNumber(View view) {
TextView t = (TextView) findViewById(R.id.textView);
temp = t.getText();
if (temp.equals("0")) {
t.clearComposingText();
t.setText("3");
} else t.append("3");
}
public void fourthNumber(View view) {
TextView t = (TextView) findViewById(R.id.textView);
temp = t.getText();
if (temp.equals("0")) {
t.clearComposingText();
t.setText("4");
} else t.append("4");
}
public void fifthNumber(View view) {
TextView t = (TextView) findViewById(R.id.textView);
temp = t.getText();
if (temp.equals("0")) {
t.clearComposingText();
t.setText("5");
} else t.append("5");
}
public void sixthNumber(View view) {
TextView t = (TextView) findViewById(R.id.textView);
temp = t.getText();
if (temp.equals("0")) {
t.clearComposingText();
num1 = "";
t.setText("6");
} else t.append("6");
}
public void seventhNumber(View view) {
TextView t = (TextView) findViewById(R.id.textView);
temp = t.getText();
if (temp.equals("0")) {
t.clearComposingText();
t.setText("7");
} else t.append("7");
}
public void eightNumber(View view) {
TextView t = (TextView) findViewById(R.id.textView);
temp = t.getText();
if (temp.equals("0")) {
t.clearComposingText();
t.setText("8");
} else t.append("8");
}
public void ninthNumber(View view) {
TextView t = (TextView) findViewById(R.id.textView);
temp = t.getText();
if (temp.equals("0")) {
t.clearComposingText();
t.setText("9");
} else t.append("9");
}
public void pressZero(View view) {
TextView t = (TextView) findViewById(R.id.textView);
temp = t.getText();
if (!temp.equals("0")) {
t.append("0");
}
}
public void goBack(View view){
TextView t = (TextView)findViewById(R.id.textView);
temp = t.getText();
if(!temp.equals("0")) {
if(n1.length()==1)
{
number1=0;
t.setText("");
out = String.valueOf(number1);
t.append(out);
}
else {
num1 = t.getText();
n1 = num1.toString();
n1 = n1.substring(0, n1.length() - 1);
number1 = Integer.parseInt(n1);
t.setText("");
out = String.valueOf(number1);
t.append(out);
}
}
}
public void pressPlus(View view)
{
TextView t = (TextView) findViewById(R.id.textView);
num1=t.getText();
t.setText("0");
operation="+";
}
public void pressMinus(View view)
{
TextView t = (TextView) findViewById(R.id.textView);
num1=t.getText();
t.setText("0");
operation="-";
}
public void pressMultiplication(View view)
{
TextView t = (TextView) findViewById(R.id.textView);
num1=t.getText();
t.setText("0");
operation="*";
}
public void pressDivision(View view)
{
TextView t = (TextView) findViewById(R.id.textView);
num1=t.getText();
t.setText("0");
operation="/";
}
public void pressProcent(View view)
{
TextView t = (TextView) findViewById(R.id.textView);
num1=t.getText();
n1 = num1.toString();
number1 = Integer.parseInt(n1);
output = number1 / x;
t.setText("");
out= String.valueOf(output);
out = out.indexOf(".")< 0 ? out : out.replaceAll("0*$", "").replaceAll("\\.$", "");
t.append(out);
}
public void pressDot(View view)
{
TextView t = (TextView) findViewById(R.id.textView);
doot=t.toString();
if (doot.contains(dooot))t.append(".");
else t.append("");
}
public void nrClear(View view)
{
TextView t = (TextView)findViewById(R.id.textView);
t.setText("0");
}
public void pressEquals(View view)
{
if(operation.equals("+"))
{
TextView t = (TextView)findViewById(R.id.textView);
num2 = t.getText();
n1 = num1.toString();
n2 = num2.toString();
number1 = Integer.parseInt(n1);
number2 = Integer.parseInt(n2);
output = number1 + number2;
}
if(operation.equals("-"))
{
TextView t = (TextView)findViewById(R.id.textView);
num2 = t.getText();
n1 = num1.toString();
n2 = num2.toString();
number1 = Integer.parseInt(n1);
number2 = Integer.parseInt(n2);
output = number1 - number2;
}
if(operation.equals("*"))
{
TextView t = (TextView)findViewById(R.id.textView);
num2 = t.getText();
n1 = num1.toString();
n2 = num2.toString();
number1 = Integer.parseInt(n1);
number2 = Integer.parseInt(n2);
output = number1 * number2;
}
if(operation.equals("/"))
{
TextView t = (TextView)findViewById(R.id.textView);
num2 = t.getText();
n1 = num1.toString();
n2 = num2.toString();
number1 = Integer.parseInt(n1);
number2 = Integer.parseInt(n2);
output = number1 / number2;
}
TextView t = (TextView)findViewById(R.id.textView);
t.setText("");
out= String.valueOf(output);
out = out.indexOf(".")< 0 ? out : out.replaceAll("0*$", "").replaceAll("\\.$", "");
t.append(out);
}
}
字符串操作;
字符序列num1;
字符序列num2;
文本视图t;
字符串n1;
n2串;
弦嘟嘟;
双数1;
双数2;
双输出;
串出;
焦序温度;
查特序列;
int x;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
操作=”;
num2=“”;
n1=“”;
n2=“”;
doot=“”;
数字1=1;
数字2=0;
输出=0;
out=“”;
温度=”;
x=100;
dooot=“.”;
}
公共void firstNumber(视图){
TextView t=(TextView)findViewById(R.id.TextView);
temp=t.getText();
如果(温度等于(“0”)){
t、 setText(“1”);
}else t.附加(“1”);
}
公共无效第二编号(视图){
TextView t=(TextView)findViewById(R.id.TextView);
temp=t.getText();
如果(温度等于(“0”)){
t、 setText(“2”);
}else t.附加(“2”);
}
公共无效第三个编号(视图){
TextView t=(TextView)findViewById(R.id.TextView);
temp=t.getText();
如果(温度等于(“0”)){
t、 clearComposingText();
t、 setText(“3”);
}else t.附加(“3”);
}
第四号公共空间(视图){
TextView t=(TextView)findViewById(R.id.TextView);
temp=t.getText();
如果(温度等于(“0”)){
t、 clearComposingText();
t、 setText(“4”);
}else t.附加(“4”);
}
公共无效第五个编号(视图){
TextView t=(TextView)findViewById(R.id.TextView);
temp=t.getText();
如果(温度等于(“0”)){
t、 clearComposingText();
t、 setText(“5”);
}其他t.附加(“5”);
}
公共无效第六个编号(视图){
TextView t=(TextView)findViewById(R.id.TextView);
temp=t.getText();
如果(温度等于(“0”)){
t、 clearComposingText();
num1=“”;
t、 setText(“6”);
}其他t.附加(“6”);
}
第七号公共空间(视图){
TextView t=(TextView)findViewById(R.id.TextView);
temp=t.getText();
如果(温度等于(“0”)){
t、 clearComposingText();
t、 setText(“7”);
}else t.附加(“7”);
}
公共无效八号(视图){
TextView t=(TextView)findViewById(R.id.TextView);
temp=t.getText();
如果(温度等于(“0”)){
t、 clearComposingText();
t、 setText(“8”);
}其他t.附加(“8”);
}
公共无效编号(视图){
TextView t=(TextView)findViewById(R.id.TextView);
temp=t.getText();
如果(温度等于(“0”)){
t、 clearComposingText();
t、 setText(“9”);
}其他t.附加(“9”);
}
公共无效按零(查看){
TextView t=(TextView)findViewById(R.id.TextView);
temp=t.getText();
如果(!temp.等于(“0”)){
t、 附加(“0”);
}
}
公共作废戈巴克(视图){
TextView t=(TextView)findViewById(R.id.TextView);
temp=t.getText();
如果(!temp.等于(“0”)){
如果(n1.length()==1)
{
数字1=0;
t、 setText(“”);
out=字符串的值(number1);
t、 附加(输出);
}
否则{
num1=t.getText();
n1=num1.toString();
n1=n1.子字符串(0,n1.长度()-1);
number1=整数.parseInt(n1);
t、 setText(“”);
out=字符串的值(number1);
t、 附加(输出);
}
}
}
public void pressPlus(查看)
{
TextView t=(TextView)findViewById(R.id.TextView);
num1=t.getText();
t、 setText(“0”);
操作=“+”;
}
公共空按减号(查看)
{
TextView t=(TextView)findViewById(R.id.TextView);
num1=t.getText();
t、 setText(“0”);
操作=“-”;
}
公共空间(视图)
{
TextView t=(TextView)findViewById(R.id.TextView);
num1=t.getText();
t、 setText(“0”);
操作=“*”;
}
公共部门(视图)
{
TextView t=(TextView)findViewById(R.id.TextView);
num1=t.getText();
t、 setText(“0”);
操作=“/”;
}
公共无效pressProcent(视图)
{
TextView t=(TextView)findViewById(R.id.TextView);
num1=t.getText();
n1=num1.toString();
number1=整数.parseInt(n1);
输出=数字1/x;
t、 setText(“”);
out=字符串.valueOf(输出);
out=out.indexOf(“.”<0?out:out.replaceAll(“0*$”).replaceAll(“\\.$”,”);
t、 附加(输出);
}
公共空压点(视图)
{
TextView t=(TextView)findViewById(R.id.TextView);
doot=t.toString();
如果(doot.包含(doot))t.追加(“.”);
else t.附加(“”);
}
公共无效nrClear(视图)
{
TextView t=(TextView)findViewById(R.id.TextView);
t、 setText(“0”);
}
public void pressEquals(视图)
{
if(操作.等于(“+”))
{
TextView t=(TextView)findViewById(R.id.TextView);
num2=t.getText();
n1=num1.toString();
n2=num2.toString();
number1=整数.parseInt(n1);
number2=整数.parseInt(n2);
输出=number1+number2;
}
if(运算.等于(“-”)
{
TextView t=(TextView)findViewById(R.id.TextView);
num2=t.getText();
n1=num1.toString();
n2=num2.toString();
number1=整数.parseInt(n1);
number2=整数.parseInt(n2);
输出=数字1
public void pressDot(View view)
{
TextView t = (TextView) findViewById(R.id.textView);
doot=t.getText().toString(); //should be this
if (doot.contains(dooot))t.append(".");
else t.append("");
}
public void eightNumber(View view) {
TextView t = (TextView) findViewById(R.id.textView);
temp = t.getText();
temp = temp + "8"
//convert the input number to double to avoid extra zero before digits (05)
double tempNumber = Double.parseDouble(temp);
//convert our double number back to string, to use it in textView.setText method
temp = String.valueOf(tempNumber);
//to avoid redundant ".0" after integer numbers:
//if 2 last digits of the number is .0, we delete it
if(temp.lastIndexOf(".0") == temp.length() - 2){
temp = temp.substring(0, temp.length() - 2);
}
t.setText(temp);
}
public void pressDot(View view)
{
TextView t = (TextView) findViewById(R.id.textView);
doot=t.toString();
if (!doot.contains(dooot)) {
t.append(".");
}
}