我刚开始构建" SIMPLE CALCULATOR"与"两个JAVA CLASSES"一个是MainActivity,另一个是显示答案。
我的代码中没有任何错误,但是当我运行应用时,按钮无法点击。我这样做了3个小时仍然无法正常工作。
public class MainActivity extends Activity {
public static String SUM_DISPLAY ="com.example.calculatorrm.DISPLAY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button listener = (Button)findViewById(R.id.add);
final Intent intent =new Intent (this,DisplayAnswerActivity.class);
listener.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText e1 = (EditText)findViewById(R.id.fistvalue);
EditText e2 = (EditText)findViewById(R.id.secondvalue);
// For displaying answer
int firstvalue =Integer.parseInt(e1.getText().toString());
int secondvalue = Integer.parseInt(e2.getText().toString());
int sum = firstvalue + secondvalue;
String display = Integer.toString(sum);
intent.putExtra(SUM_DISPLAY,display);
}
});
}
答案 0 :(得分:0)
此行之后调用startActivity
方法:
intent.putExtra(SUM_DISPLAY,display);
像这样:
startActivity(intent);
答案 1 :(得分:0)
public class MainActivity extends Activity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Button listener = (Button)findViewById(R.id.add);
listener.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText e1 = (EditText)findViewById(R.id.fistvalue);
EditText e2 = (EditText)findViewById(R.id.secondvalue);
// For displaying answer
int firstvalue =Integer.parseInt(e1.getText().toString());
int secondvalue = Integer.parseInt(e2.getText().toString());
int sum = firstvalue + secondvalue;
String display = Integer.toString(sum);
Intent intent =new Intent (context, DisplayAnswerActivity.class);
intent.putExtra("Sum",display);
startActivity(intent);
}
});
}
在活动DisplayAnswerActivity
中获取Intent
,即可在onCreateMethod()
中获得此总和值。
Intent i = getIntent();
String output = i.getStringExtra("Sum");