Чтобы ответить на этот вопрос, я новичок в кодировании и создании калькулятора, чтобы познакомиться с Android Studio. Так вот почему код выглядит неполным. В настоящее время я тестирую его только с цифрами восемь и девять и знаком плюс.
Мой вопрос: как мне выйти из этого для l oop после нажатия на plusBtn, чтобы число вводило go во второй текстовый просмотр? Если есть лучший способ, чем использовать для l oop, это тоже хорошо.
package com.example.calculator3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button num9;
private Button num8;
private TextView firstNumber;
private TextView relation;
private TextView secondNumber;
private Button plusBtn;
private TextView result;
private Button equalsBtn;
//private double val1;
//private double val2;
// private final char ADDITION = '+';
//private final char EQUALS = '=';
// private char ACTION;
//public boolean isPlusClicked = false;
//String relationStr = relation.getText().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num9 = (Button) findViewById(R.id.num9);
num8 = (Button) findViewById(R.id.num8);
firstNumber = (TextView) findViewById(R.id.firstNumber);
relation = (TextView) findViewById(R.id.relation);
secondNumber = (TextView) findViewById(R.id.secondNumber);
plusBtn = (Button) findViewById(R.id.plusBtn);
result = (TextView) findViewById(R.id.result);
equalsBtn = (Button) findViewById(R.id.equalsBtn);
if (relation.getText().length() == 0){
num9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstNumber.setText(firstNumber.getText().toString() + "9");
}
});
num8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstNumber.setText(firstNumber.getText().toString() + "8");
}
});
plusBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
relation.setText("+");
}
});
} else {
num9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
secondNumber.setText(secondNumber.getText().toString() + "9");
}
});
num8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
secondNumber.setText(secondNumber.getText().toString() + "8");
}
});
plusBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
relation.setText(relation.getText().toString() + "+");
}
});
}
}
}
Макет:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<Button
android:id="@+id/num9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginBottom="152dp"
android:text="9"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/num8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="36dp"
android:layout_marginRight="36dp"
android:layout_marginBottom="140dp"
android:text="8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/plusBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="76dp"
android:text="+"
app:layout_constraintBottom_toTopOf="@+id/result"
app:layout_constraintEnd_toStartOf="@+id/num8"
app:layout_constraintHorizontal_bias="0.518"
app:layout_constraintStart_toEndOf="@+id/num9" />
<Button
android:id="@+id/equalsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
app:layout_constraintBottom_toTopOf="@+id/plusBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/secondNumber" />
<TextView
android:id="@+id/firstNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="firstNumber"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/relation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="184dp"
android:hint="relation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/firstNumber" />
<TextView
android:id="@+id/secondNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="148dp"
android:hint="secondNumber"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/relation" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="48dp"
android:hint="result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent" />
'''
Ответ:
'''
public class MainActivity extends AppCompatActivity {
private Button num9;
private Button num8;
private TextView firstNumber;
private TextView relation;
private TextView secondNumber;
private Button plusBtn;
private TextView result;
private Button equalsBtn;
//private double val1;
//private double val2;
// private final char ADDITION = '+';
//private final char EQUALS = '=';
// private char ACTION;
//public boolean isPlusClicked = false;
//String relationStr = relation.getText().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num9 = (Button) findViewById(R.id.num9);
num8 = (Button) findViewById(R.id.num8);
firstNumber = (TextView) findViewById(R.id.firstNumber);
relation = (TextView) findViewById(R.id.relation);
secondNumber = (TextView) findViewById(R.id.secondNumber);
plusBtn = (Button) findViewById(R.id.plusBtn);
result = (TextView) findViewById(R.id.result);
equalsBtn = (Button) findViewById(R.id.equalsBtn);
num9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (relation.getText().length() == 0) {
firstNumber.setText(firstNumber.getText().toString() + "9");
} else {
secondNumber.setText(secondNumber.getText().toString() + "9");
}
}
});
num8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (relation.getText().length() == 0) {
firstNumber.setText(firstNumber.getText().toString() + "8");
} else {
secondNumber.setText(secondNumber.getText().toString() + "8");
}
}
});
plusBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (relation.getText().length() == 0) {
relation.setText(relation.getText().toString() + "+");
}
}
});
}
}
'''