Я только начал осваивать Android,и в качестве тренировки решил переписать вот этот простенький калькулятор -
cybern.ru/calcjava.html на Android в виде графического приложения.В Android Studio сделал элементарный графический интерфейс и переписал приложение следующим образом:
public class MainActivity extends AppCompatActivity {
public double FirstValue;
String operator;
TextView MyView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyView = (TextView)findViewById(R.id.MyView);
}
public void ClickOne(View view){
MyView.setText(MyView.getText() + "1");
}
public void ClickTwo(View view){
MyView.setText(MyView.getText() + "2");
}
public void ClickThree(View view){
MyView.setText(MyView.getText() + "3");
}
public void ClickFour(View view){
MyView.setText(MyView.getText() + "4");
}
public void ClickFive(View view){
MyView.setText(MyView.getText() + "5");
}
public void ClickSix(View view){
MyView.setText(MyView.getText() + "6");
}
public void ClickSeven(View view){
MyView.setText(MyView.getText() + "7");
}
public void ClickEight(View view){
MyView.setText(MyView.getText() + "8");
}
public void ClickNine(View view){
MyView.setText(MyView.getText() + "9");
}
public void ClickNull(View view){
MyView.setText(MyView.getText() + "0");
}
public void ClickN(View view){
CharSequence FirstValur_str = MyView.getText();
String FirstValurString = String.valueOf(FirstValur_str);
FirstValue = Double.valueOf(FirstValurString);
MyView.setText("");
operator = "/";
}
public void ClickM(View view){
CharSequence FirstValur_str = MyView.getText();
String FirstValurString = String.valueOf(FirstValur_str);
FirstValue = Double.valueOf(FirstValurString);
MyView.setText("");
operator = "*";
}
public void ClickPlus(View view){
CharSequence FirstValur_str = MyView.getText();
String FirstValurString = String.valueOf(FirstValur_str);
FirstValue = Double.valueOf(FirstValurString);
MyView.setText("");
operator = "+";
}
public void ClickMinus(View view){
CharSequence FirstValur_str = MyView.getText();
String FirstValurString = String.valueOf(FirstValur_str);
FirstValue = Double.valueOf(FirstValurString);
MyView.setText("");
operator = "-";
}
public void ClickClear(View view){
FirstValue = 0;
MyView.setText("");
}
public void ClickRes(View view){
CharSequence FirstVaur_str = MyView.getText();
String FirstValueString = String.valueOf(FirstVaur_str);
double NextValue = Double.valueOf(FirstValueString);
if ("+".equals(operator)){
String ResultGetTextString = String.valueOf(FirstVaur_str);
double Result = Double.valueOf(ResultGetTextString) + NextValue;
MyView.setText((int) Result);
}
if("-".equals(operator)){
String ResultGetTextString = String.valueOf(FirstVaur_str);
double Result = Double.valueOf(ResultGetTextString) - NextValue;
MyView.setText((int) Result);
}
if ("*".equals(operator)){
String ResultGetTextString = String.valueOf(FirstVaur_str);
double Result = Double.valueOf(ResultGetTextString) * NextValue;
MyView.setText((int) Result);
}
if ("/".equals(operator)){
String ResultGetTextString = String.valueOf(FirstVaur_str);
double Result = Double.valueOf(ResultGetTextString) / NextValue;
MyView.setText((int) Result);
}
}
}
Кнопки DEL,% и скобки пока еще не реализовал.При нажатии на "=" после операции прибавить/отнять/делить/умножить приложение крашится.В чем проблема?