[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對象來啟動ResultActivity  
            Activity_03.this.startActivity(intent); 
        } 
         
    } 
 

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
   Activity_03.this.startActivity(intent);
  }
  
 }

}

 

 

 

 

2."Activity_03\src\yan\activity_02\ResultActivity.java"

[java]
package yan.activity_03; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 
 
// 1.接收從Activity_03當中傳遞過來的值  
// 2.計算兩個值的積  
// 3.將計算的結果顯示在Activity上  
 
public class ResultActivity extends Activity{ 
    private TextView resultView; 
    private int result; 
     
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.result); 
         
        resultView = (TextView)findViewById(R.id.result); 
        // 得到Intent對象當中的值  
        Intent intent = getIntent(); 
        String factorOneStr = intent.getStringExtra("one"); 
        String factorTwoStr = intent.getStringExtra("two"); 
        System.out.println(" 225 === "); 
         
        // 判斷輸入是否為空.  
        if(factorOneStr.equals("") || factorTwoStr.equals("")) 
            result=0; 
        else 
        { 
            int factorOneInt = Integer.parseInt(factorOneStr); 
            int factorTwoInt = Integer.parseInt(factorTwoStr); 
            // 計算兩個值的積  
            result = factorOneInt * factorTwoInt; 
        } 
 
        // 顯示結果到界面  
        resultView.setText(result + ""); 
    } 

package yan.activity_03;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

// 1.接收從Activity_03當中傳遞過來的值
// 2.計算兩個值的積
// 3.將計算的結果顯示在Activity上

public class ResultActivity extends Activity{
 private TextView resultView;
 private int result;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.result);
  
  resultView = (TextView)findViewById(R.id.result);
  // 得到Intent對象當中的值
  Intent intent = getIntent();
  String factorOneStr = intent.getStringExtra("one");
  String factorTwoStr = intent.getStringExtra("two");
  System.out.println(" 225 === ");
  
  // 判斷輸入是否為空.
  if(factorOneStr.equals("") || factorTwoStr.equals(""))
   result=0;
  else
  {
   int factorOneInt = Integer.parseInt(factorOneStr);
   int factorTwoInt = Integer.parseInt(factorTwoStr);
   // 計算兩個值的積
   result = factorOneInt * factorTwoInt;
  }

  // 顯示結果到界面
  resultView.setText(result + "");
 }
}

3."Activity_03\res\layout\activity_03.xml"

 

 

 

[java]
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
   <EditText 
       android:id="@+id/factorOne" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 
   <TextView 
       android:id="@+id/symbol" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@+string/hello_world" 
       /> 
   <EditText 
       android:id="@+id/factorTwo" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 
    <Button 
        android:id="@+id/calculate" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        /> 
</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   <EditText
       android:id="@+id/factorOne"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       />
   <TextView
       android:id="@+id/symbol"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@+string/hello_world"
       />
   <EditText
       android:id="@+id/factorTwo"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       />
    <Button
        android:id="@+id/calculate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

 

 

 

4."Activity_03\res\layout\result.xml"
[html]
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
 <TextView 
       android:id="@+id/result" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 
</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView
       android:id="@+id/result"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       />
</LinearLayout>
5."Activity_03\AndroidManifest.xml"
[java]
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="yan.activity_03" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
    <uses-sdk 
        android:minSdkVersion="4" 
        android:targetSdkVersion="4" /> 
 
    <application 
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" 
        android:theme="@style/AppTheme" > 
        <activity 
            android:name="yan.activity_03.Activity_03" 
            android:label="@string/app_name" > 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
        <activity android:name="yan.activity_03.ResultActivity" 
            android:label="@string/result" > 
        </activity> 
    </application> 
 
</manifest> 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="yan.activity_03"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="4" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="yan.activity_03.Activity_03"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="yan.activity_03.ResultActivity"
            android:label="@string/result" >
        </activity>
    </application>

</manifest>
6."Activity_03\res\values\strings.xml"

[html]
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="app_name">Activity_03</string> 
    <string name="hello_world">Hello world!</string> 
    <string name="menu_settings">Settings</string> 
    <string name="symbol">乘以</string> 
    <string name="calculate">計算</string> 
    <string name="result">結果</string> 
    <string name="exit">退出</string> 
    <string name="about">關於</string> 
</resources> 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Activity_03</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
 <string name="symbol">乘以</string>
 <string name="calculate">計算</string>
 <string name="result">結果</string>
 <string name="exit">退出</string>
 <string name="about">關於</string>
</resources>

 

 

 

 

 

 

 

 

三、效果展示:

 
 

 

 

點擊"計算"按鈕,之後,跳轉到ResultActivity–>>

 

 

 

 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *