TextView
EditText
Button
Menu
//1.在Activity03当中,要声明四个控件 在main.xml声明。
//2.要为其中的两个控件设置显示的值
//3.创建一个监听器类,监听按钮按下的动作
//4.将监听器类的对象,绑定在按钮对象上
//2.要为其中的两个控件设置显示的值
//3.创建一个监听器类,监听按钮按下的动作
//4.将监听器类的对象,绑定在按钮对象上
1在main.xml声明四个控件
<EditText
android:id="@+id/factorOne"
android:layout_width="fill_parent"
android:layout_height="vrap_content"
/>
<EditText
android:id="@+id/symbol" //除号
android:layout_width="fill_parent"
android:layout_height="vrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/factorTwo"
android:layout_width="fill_parent"
android:layout_height="vrap_content"
/>
<Button
android:id="@+id/calculate"
android:layout_width="fill_parent"
android:layout_height="vrap_content"
/>
2 然后要在Activity把四个控件取出来
private EditText factorOne ;
private EditText factorTwo;
private TextView symbol;
private Button calculate;
private EditText factorTwo;
private TextView symbol;
private Button calculate;
3 用findViewByID这个方法 把四个控件对象都取出来
factorOne = (EditText)findViewById(R.id.factorOne);
factorTwo = (EditText)findViewById(R.id.factorTwo);
symbol = (TextView)findViewById(R.id.symbol);
calculate = (Button)findViewById(R.id.calculate);
factorTwo = (EditText)findViewById(R.id.factorTwo);
symbol = (TextView)findViewById(R.id.symbol);
calculate = (Button)findViewById(R.id.calculate);
4 为symbol和calculate设置显示的值
// symbol.setText("乘以");
// calculate.setText("计算");
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
// calculate.setText("计算");
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
5 编写监听器
1取得factorOne 和factorTwo 的值
2把取得的值放到Intent键值对当中。
3用Intent启动ResultActivity。
class CalculateListener implements OnClickListener{
@Override
public void onClick(View v) {
//取得两个EditText控件的值
String factorOneStr = factorOne.getText().toString();
String factorTwoStr = factorTwo.getText().toString();
//将这两个值存放到Intent对象当中
Intent intent = new Intent();
intent.putExtra("one",factorOneStr);
intent.putExtra("two",factorTwoStr);
intent.setClass(Activity03.this, ResultActivity.class);
//使用这个Intent对象来启动ResultActivity
Activity03.this.startActivity(intent);
public void onClick(View v) {
//取得两个EditText控件的值
String factorOneStr = factorOne.getText().toString();
String factorTwoStr = factorTwo.getText().toString();
//将这两个值存放到Intent对象当中
Intent intent = new Intent();
intent.putExtra("one",factorOneStr);
intent.putExtra("two",factorTwoStr);
intent.setClass(Activity03.this, ResultActivity.class);
//使用这个Intent对象来启动ResultActivity
Activity03.this.startActivity(intent);
4 绑定!
将监听器的对象绑定到按钮对象上面
calculate.setOnClickListener(new CalculateListener());
5编写ResultActivity
//1.接受从Activity03当中所传递的值(键值对)
//2.计算两个值的积
//3.将计算的结果显示在Activity上
//2.计算两个值的积
//3.将计算的结果显示在Activity上
6 在resul.xml加TextView
<EditText
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="vrap_content"
/>
7 同样,先声明TextView对象,也就是把TextView取出来
private TextView resultView;
8 用findViewByID这个方法把控件对象取出来
resultView = (TextView)findViewById(R.id.result);
9 得到Intent对象当中的值
Intent intent = getIntent();
String factorOneStr = intent.getStringExtra("one");
String factorTwoStr = intent.getStringExtra("two");
int factorOneInt = Integer.parseInt(factorOneStr);//字符串转整形
int factorTwoInt = Integer.parseInt(factorTwoStr);//字符串转整形
String factorOneStr = intent.getStringExtra("one");
String factorTwoStr = intent.getStringExtra("two");
int factorOneInt = Integer.parseInt(factorOneStr);//字符串转整形
int factorTwoInt = Integer.parseInt(factorTwoStr);//字符串转整形
10 计算两个值的积
int result = factorOneInt * factorTwoInt;
11 显示结果
resultView.setText(result + ""); //result是整形结果,要转化成字符串。JAVA规定,加号两端有
一端为字符串(""为空字符串),另外一端result也会转化为字
符串,结果为字符串。
12 总体完成。再添加Menu控件,当点击手机Menu按钮的时候执行函数。
复写方法:onCreateOptionsMenu //复写函数:右键——source——Override/Implement Methods
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 1, 1, R.string.exit); // 参数 (菜单所处组的名字 ID,当前菜单的ID ItemId,
menu.add(0, 1, 1, R.string.exit); // 参数 (菜单所处组的名字 ID,当前菜单的ID ItemId,
排序,
按钮显示的内容)显示内容要写在strings.xml当中
menu.add(0,2,2,R.string.about);
return super.onCreateOptionsMenu(menu);
}
menu.add(0,2,2,R.string.about);
return super.onCreateOptionsMenu(menu);
}
复写onOptionsItemSelected //当客户点击菜单当中的某一个选项时,会调用该方法
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == 1){
finish();
}
return super.onOptionsItemSelected(item);
}
if(item.getItemId() == 1){
finish();
}
return super.onOptionsItemSelected(item);
}
Activity03
ResultActivity