我目前正在使用android的第一步。我正在开发一个非常简单的计算器,其中我有两个活动,一个主要活动和一个结果活动。在这个结果活动中,我有一个返回按钮,用于主要活动。问题是,当我返回主活动时,文本字段上的值仍然存在。我怎么能这样做,当我从结果活动返回时,主活动上的字段被清除?
以下是我主要活动的代码:
public class MainActivity extends Activity implements OnCheckedChangeListener{
private static final int REQUEST_CODE = 1;
private EditText number1, number2;
private Button btn1;
private Intent intent;
private String operation;
private RadioGroup RadioGroupOperation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//faz a ligação entre a aplicação e o layout
number1 = (EditText)findViewById(R.id.editText1);
number2 = (EditText)findViewById(R.id.editText2);
btn1 = (Button)findViewById(R.id.calcular);
intent = new Intent( this, ResultActivity.class);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//insere na intent os dois numeros digitados pelo utilizador
intent.putExtra("number1", number1.getText().toString());
intent.putExtra("number2", number2.getText().toString());
intent.putExtra("operation", operation);
startActivityForResult(intent, REQUEST_CODE);
}
});
RadioGroupOperation = (RadioGroup)findViewById(R.id.radioGroup1);
RadioGroupOperation.setOnCheckedChangeListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.soma:
operation="+";
break;
case R.id.subtracao:
operation="-";
break;
case R.id.multiplicacao:
operation="*";
break;
case R.id.divisao:
operation="/";
break;
}
Toast.makeText(MainActivity.this, operation, Toast.LENGTH_SHORT).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if( resultCode == RESULT_OK && requestCode == REQUEST_CODE ){
if(data.hasExtra("result")){
Toast.makeText(this, "O resultado da operação anterior foi:" + data.getExtras().getString("result") , Toast.LENGTH_LONG).show();
}
//testing
/*if(data.hasExtra("number1") && data.hasExtra("number2")){
number1.setText(0);
number2.setText(0);
}*/
//end testing
}
}
}
我的第二项活动(结果):
public class ResultActivity extends Activity {
private TextView textView1;
private Intent intent;
private String number1, number2, operation;
private double result;
private Button btn1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
btn1 = (Button)findViewById(R.id.voltar);
intent = getIntent();
//armazena os numeros digitados
number1 = intent.getStringExtra("number1");
number2 = intent.getStringExtra("number2");
operation = intent.getStringExtra("operation");
//Efectua calculos
try {
if(operation.equals("+"))
result = (Double.parseDouble(number1) + Double.parseDouble(number2));
else if(operation.equals("-"))
result = (Double.parseDouble(number1) - Double.parseDouble(number2));
else if(operation.equals("*"))
result = (Double.parseDouble(number1) * Double.parseDouble(number2));
else if(operation.equals("/"))
result = (Double.parseDouble(number1) / Double.parseDouble(number2));
textView1 = (TextView)findViewById(R.id.textView1);
textView1.setText("O resultado é " + String.valueOf(result));
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
catch(Exception e){
Toast.makeText(this, "Ocorreu um erro!", Toast.LENGTH_SHORT).show();
this.finish();
}
}
@Override
public void finish(){
Intent data = new Intent();
data.putExtra("result", String.valueOf(result));
//testing
//data.putExtra("number1", 0);
//data.putExtra("number2", 0);
//end testing
setResult(RESULT_OK, data);
super.finish();
}
}
我已经尝试过这样做,如果你看到//测试和//结束测试之间的块,但结果是当我试图回到第一个活动时我崩溃了应用程序。
我对如何做到这一点有任何想法?
提前致谢, CT
答案 0 :(得分:1)
在onActivityResult
函数内的第一个活动中,您应该重置两个editText
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if( resultCode == RESULT_OK && requestCode == REQUEST_CODE ){
if(data.hasExtra("result")){
Toast.makeText(this, "O resultado da operação anterior foi:" + data.getExtras().getString("result") , Toast.LENGTH_LONG).show();
number1 = (EditText)findViewById(R.id.editText1);
number2 = (EditText)findViewById(R.id.editText2);
number1.setText(0);
number2.setText(0);
}
//testing
/*if(data.hasExtra("number1") && data.hasExtra("number2")){
number1.setText(0);
number2.setText(0);
}*/
//end testing
}
}
您的测试代码无法正常工作,因为两个EditText
变量未初始化。当你回来时,你在onCreate
内进行的inizialization会丢失。
答案 1 :(得分:1)
您也可以添加意图来加载您在记事本应用程序上工作的第一个活动:)
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(),MainActiviy.class);
startActivity(myIntent);
finish();
}
});
答案 2 :(得分:1)
除了将文本设置为0之外,一切都是正确的.RouitText.setText只接受字符串值。
将测试代码更改为
if(data.hasExtra("number1") && data.hasExtra("number2")){
number1.setText("0");
number2.setText("0");
}
只需要更改setText(“0”)