我想创建一个以这种方式运作的“计算器”:当我在MainActivity
时,按下TextView
,Dialog
(或Theme.DeviceDefault.Light.Dialog
的活动}主题或类似的东西)用键盘打开,我可以在其中进行操作,结果将更改TextView
的文本。结果将是这样的:
问题是每次按“+”或“ - ”并且应用程序关闭IncreaseReduce
活动时,MainActivity
会重新初始化并{{1方法是调用(我推测)。例如,如果我想添加5,则应用程序将5添加到默认值(在本例中为1000)。每次都会发生这种情况,因此我不能执行以下操作:previous_result + 5 + 10(结果1015)。结果将是1010.我该如何解决?此外,我更喜欢使用对话,但我不喜欢如何
这是我的代码:
MainActivity.class
onCreate
EncreaseReduce.class
public class MainActivity extends AppCompatActivity {
private TextView textView;
private String toEncreaseReduce = "";
private int operation = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, IncreaseReduce.class);
startActivity(intent);
}
});
Bundle b = getIntent().getExtras();
if(b != null) {
toEncreaseReduce = b.getString("to");
operation = b.getInt("operation");
}
if(operation != -1 && toEncreaseReduce != "")
operate(toEncreaseReduce,operation);
}
public void operate(String toEncreaseReduce, int operation)
{
long lpInt;
String lpString;;
long op;
lpString = textView.getText().toString();
lpInt = Long.parseLong(lpString);
op = Long.parseLong(toEncreaseReduce);
if (operation == 1)
lpInt = lpInt + op;
if(operation == 2)
lpInt = lpInt - op;
textView.setText("" + Long.toString(lpInt));
}
}
清单
public class IncreaseReduce extends Activity implements View.OnClickListener{
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
private Button buttonm;
private Button button0;
private Button buttonp;
private String lp;
private String number = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.increase_reduce_layout);
button0 = (Button) findViewById(R.id.b0);
button1 = (Button) findViewById(R.id.b1);
button2 = (Button) findViewById(R.id.b2);
button3 = (Button) findViewById(R.id.b3);
button4 = (Button) findViewById(R.id.b4);
button5 = (Button) findViewById(R.id.b5);
button6 = (Button) findViewById(R.id.b6);
button7 = (Button) findViewById(R.id.b7);
button8 = (Button) findViewById(R.id.b8);
button9 = (Button) findViewById(R.id.b9);
buttonp = (Button) findViewById(R.id.bp);
buttonm = (Button) findViewById(R.id.bm);
button0.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
buttonp.setOnClickListener(this);
buttonm.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case(R.id.b0):
number = number + "0";
setTitle(number);
break;
case(R.id.b1):
number = number + "1";
setTitle(number);
break;
case(R.id.b2):
number = number + "2";
setTitle(number);
break;
case(R.id.b3):
number = number + "3";
setTitle(number);
break;
case(R.id.b4):
number = number + "4";
setTitle(number);
break;
case(R.id.b5):
number = number + "5";
setTitle(number);
break;
case(R.id.b6):
number = number + "6";
setTitle(number);
break;
case(R.id.b7):
number = number + "7";
setTitle(number);
break;
case(R.id.b8):
number = number + "8";
setTitle(number);
break;
case(R.id.b9):
number = number + "9";
setTitle(number);
break;
case(R.id.bp):
passage(1, number);
break;
case(R.id.bm):
passage(2, number);
break;
}
}
public void passage(int operation, String toIncreaseReduce)
{
/*
* 1: add
* 2: reduce
*/
Intent intent = new Intent(IncreaseReduce.this, MainActivity.class);
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (toIncreaseReduce != "") {
Bundle b = new Bundle();
b.putString("to", number);
b.putInt("operation",1);
intent.putExtras(b); //Put your id to your next Intent
}
startActivity(intent);
finish();
}
}
<?xml version="1.0" encoding="utf-8"?>