[Android开发学习21]写一个简单的乘法计算器,并添加退出菜单
一、目标和即将学到的知识:
1.目标:
写一个简单的乘法计算器,并添加退出菜单
2.即将需要或者学习到的知识:
①.TextView显示文本
②.EditText编辑框
③.Button按钮
④.Menu菜单
3.基础知识:
① setText函数中,连接符两端,有一个端是字符串,则另外一端也会被转为字符串。
[java]
private TextView resultView;
private int result;
...
resultView.setText(result + "");
private TextView resultView;
private int result;
...
resultView.setText(result + "");
② 判断某字符串是否为空:
if(factorOneStr.equals(""))
③ 字符串转整数:
int factorOneInt = Integer.parseInt(factorOneStr);
④ 添加菜单:
为主程序添加菜单显示: onCreateOptionsMenu(重载函数)
为菜单添加响应事件: onOptionsItemSelected(重载函数)
[java]
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1){
finish(); // 结束当前Activity.
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0,1,1,R.string.exit);
menu.add(0,2,2,R.string.about);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1){
finish(); // 结束当前Activity.
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0,1,1,R.string.exit);
menu.add(0,2,2,R.string.about);
return super.onCreateOptionsMenu(menu);
}
3.用到的快捷键:
添加导入库 Ctrl+Shift+M
二、代码展示:
1."Activity_03\src\yan\activity_02\Activity_03.java"
[java]
package yan.activity_03;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/*
* 1.在Activity_03当中,要声明四个控件
* 2.要为其中的两个控件设置显示的值
* 3.创建一个易做图类,监听按钮按下的动作
* 4.将易做图类的对象,绑定在按钮对象上
* */
public class Activity_03 extends Activity {
private EditText factorOne;
private EditText factorTwo;
private TextView symbol;
private Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_03);
//根据控件的ID来取得代表控件的对象
factorOne = (EditText)findViewById(R.id.factorOne);
factorTwo = (EditText)findViewById(R.id.factorTwo);
symbol = (TextView)findViewById(R.id.symbol);
calculate = (Button)findViewById(R.id.calculate);
// 为symbol和calculate设置显示的值
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
// 将易做图的对象绑定到按钮对象上面
calculate.setOnClickListener(new CalculateListener());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1){
finish(); // 结束当前Activity.
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0,1,1,R.string.exit);
menu.add(0,2,2,R.string.about);
return super.onCreateOptionsMenu(menu);
}
class CalculateListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 取得两个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(Activity_03.this, ResultActivity.class);
// 使用这个Intent对象来启动R
补充:移动开发 , Android ,