0
我正在開發一個基本計算器android應用程序,我堅持在某個階段。此應用程序從textview中獲取輸入並將其存儲在一個數字中。基本計算器Android應用程序
從用戶處獲取第二個輸入時出現問題。
我正在設置用於將第一個輸入(第一個數字)爲空的字符串。使用相同的字符串,我怎樣才能使字符串爲空,並允許定義操作後的第二個輸入。那裏的任何人都可以幫助我並感激我。
預先感謝您..
public class MainActivity extends AppCompatActivity {
String answer = "";
float num1, num2;
int flag = 0;
String operation = "";
TextView textViewAnswer;
Button buttonOne;
Button buttonTwo;
Button buttonThree;
Button buttonFour;
Button buttonFive;
Button buttonSix;
Button buttonSeven;
Button buttonEight;
Button buttonNine;
Button buttonZero;
Button buttonAddition;
Button buttonSubstraction;
Button buttonMultiplication;
Button buttonDivision;
Button buttonDot;
Button buttonEqual;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewAnswer = (TextView) findViewById(R.id.textview_answer);
}
public void one(View view){
buttonOne = (Button) findViewById(R.id.button_one);
if (flag == 1) {
num1 = Float.parseFloat(textViewAnswer.getText().toString());
flag = 0;
answer = "";
}
else {
checkLength(buttonOne);
textViewAnswer.setText(answer);
}
}
public void two(View view){
buttonTwo = (Button) findViewById(R.id.button_two);
checkLength(buttonTwo);
textViewAnswer.setText(answer);
if (flag == 1) {
num1 = Float.parseFloat(textViewAnswer.getText().toString());
flag = 0;
answer = "";
}
}
public void three(View view){
buttonThree = (Button) findViewById(R.id.button_three);
checkLength(buttonThree);
textViewAnswer.setText(answer);
}
public void four(View view){
buttonFour = (Button) findViewById(R.id.button_four);
checkLength(buttonFour);
textViewAnswer.setText(answer);
}
public void five(View view){
buttonFive = (Button) findViewById(R.id.button_five);
checkLength(buttonFive);
textViewAnswer.setText(answer);
}
public void six(View view){
buttonSix = (Button) findViewById(R.id.button_six);
checkLength(buttonSix);
textViewAnswer.setText(answer);
}
public void seven(View view){
buttonSeven = (Button) findViewById(R.id.button_seven);
checkLength(buttonSeven);
textViewAnswer.setText(answer);
}
public void eight(View view){
buttonEight = (Button) findViewById(R.id.button_eight);
checkLength(buttonEight);
textViewAnswer.setText(answer);
}
public void nine(View view){
buttonNine = (Button) findViewById(R.id.button_nine);
checkLength(buttonNine);
textViewAnswer.setText(answer);
}
public void zero(View view){
buttonZero = (Button) findViewById(R.id.button_zero);
checkLength(buttonZero);
textViewAnswer.setText(answer);
}
public void dot(View view){
buttonDot = (Button) findViewById(R.id.button_dot);
checkLength(buttonDot);
textViewAnswer.setText(answer);
}
public void equal(View view){
buttonEqual = (Button) findViewById(R.id.button_equal);
// textViewAnswer.setText(buttonEqual.getText().toString());
switch (operation){
case "+" : num2 = num1 + (Float.parseFloat(textViewAnswer.getText().toString()));
answer = (String.valueOf(num2));
textViewAnswer.setText(answer);
}
}
public void addition(View view){
buttonAddition = (Button) findViewById(R.id.button_addition);
textViewAnswer.setText(buttonAddition.getText().toString());
operation = "+";
flag = 1;
}
public void substraction(View view){
buttonSubstraction = (Button) findViewById(R.id.button_substraction);
textViewAnswer.setText(buttonSubstraction.getText().toString());
operation = "-";
}
public void division(View view){
buttonDivision = (Button) findViewById(R.id.button_division);
textViewAnswer.setText(buttonDivision.getText().toString());
operation = "/";
}
public void multiplication(View view){
buttonMultiplication = (Button) findViewById(R.id.button_multiplication);
textViewAnswer.setText(buttonMultiplication.getText().toString());
operation = "*";
}
public void checkLength(Button button){
if((answer.length()) < 9)
answer += button.getText().toString();
else
Toast.makeText(this, "Cannot take more input", Toast.LENGTH_SHORT).show();
}
}
佈局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="com.example.darpan.basiccalculator.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="bottom">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Answer"
android:id="@+id/textview_answer"
android:textSize="50sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:onClick="one"/>
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"
android:onClick="two"/>
<Button
android:id="@+id/button_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
android:onClick="three"/>
<Button
android:id="@+id/button_addition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:onClick="addition"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"
android:onClick="four"/>
<Button
android:id="@+id/button_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"
android:onClick="five"/>
<Button
android:id="@+id/button_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
android:onClick="six"/>
<Button
android:id="@+id/button_substraction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:onClick="substraction"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"
android:onClick="seven"/>
<Button
android:id="@+id/button_eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"
android:onClick="eight"/>
<Button
android:id="@+id/button_nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
android:onClick="nine"/>
/>
<Button
android:id="@+id/button_multiplication"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:onClick="multiplication"/>
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_dot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."
android:onClick="dot"/>
<Button
android:id="@+id/button_zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:onClick="zero"/>
<Button
android:id="@+id/button_equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="
android:onClick="equal"/>
<Button
android:id="@+id/button_division"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:onClick="division"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
我不太明白你在說什麼。請張貼一些代碼,並嘗試更好地解釋一下。 – Vucko
請發佈一些您遇到問題的代碼。 –
社區新手..已經習慣了.. – pinku