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_03srcyanactivity_02Activity_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對象來啟動ResultActivity