【Android开发学习21】写一个简单的乘法计算器,并添加退出菜单

一、目标和即将学到的知识:

1.目标:

写一个简单的乘法计算器,并添加退出菜单


2.即将需要或者学习到的知识:

①.TextView显示文本

②.EditText编辑框

③.Button按钮     

④.Menu菜单


3.基础知识:

① setText函数中,连接符两端,有一个端是字符串,则另外一端也会被转为字符串。

private TextView resultView;
private int result;
...
resultView.setText(result + "");

 

② 判断某字符串是否为空:

       if(factorOneStr.equals(""))

③ 字符串转整数:

      int factorOneInt = Integer.parseInt(factorOneStr);

④ 添加菜单:

为主程序添加菜单显示: onCreateOptionsMenu(重载函数)
为菜单添加响应事件: onOptionsItemSelected(重载函数)

	@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"

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"

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"

<?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"
<?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"
<?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"

<?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>





三、效果展示:

【Android开发学习21】写一个简单的乘法计算器,并添加退出菜单_第1张图片


点击"计算"按钮,之后,跳转到ResultActivity-->>


【Android开发学习21】写一个简单的乘法计算器,并添加退出菜单_第2张图片


当返回之后,按手机的"菜单"键,显示如下 -->> 




其中的"退出"和“关于”就是我们自己添加进去的菜单了。

 


 

本文完整代码下载地址:http://download.csdn.net/detail/ypist/5138073

本文博客源地址:http://blog.csdn.net/ypist





你可能感兴趣的:(android)