Android初级教程Activity小案例(计算器乘法运算)

首先设置两个布局文件,一个布局文件进行输入数据,获取加法运算;另一个布局文件进行显示最终结果。Activity1启动Activity2,并传递计算结果值给Activity2.

main.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" 
    />
<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>

页面展示:

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>

界面展示:

activity03活动:


package mars.activity03;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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.在Activity03当中,要声明四个控件
//2.要为其中的两个控件设置显示的值
//3.创建一个监听器类,监听按钮按下的动作
//4.将监听器类的对象,绑定在按钮对象上
public class Activity03 extends Activity {
    /** Called when the activity is first created. */
	private EditText factorOne ;
	private EditText factorTwo;
	private TextView symbol;
	private Button calculate;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //根据控件的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("乘以");
//        calculate.setText("计算");
        symbol.setText(R.string.symbol);//这里通过引用的方式,去String文件中引用。保证了业务逻辑、视图、引用资源分开
        calculate.setText(R.string.calculate);
        //将监听器的对象绑定到按钮对象上面
        calculate.setOnClickListener(new CalculateListener());
    }
    //当客户点击MENU按钮的时候,调用该方法
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
    	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) {
		if(item.getItemId() == 1){
			finish();
		}
		return super.onOptionsItemSelected(item);
	}
	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);
		}
    }
}

resultActivity:

package mars.activity03;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
//1.接受从Activity03当中所传递的值
//2.计算两个值的积
//3.将计算的结果显示在Activity上
public class ResultActivity extends Activity{
	private TextView resultView;
	@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");
		int factorOneInt = Integer.parseInt(factorOneStr);
		int factorTwoInt = Integer.parseInt(factorTwoStr);
		//计算两个值的积
		int result = factorOneInt * factorTwoInt;
		resultView.setText(result + "");
	}

}

String.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Activity03!</string>
    <string name="app_name">activity03</string>
    <string name="resultLabel">result</string>
    <string name="symbol">乘以</string>
    <string name="calculate">计算</string>
    <string name="exit">退出</string>
    <string name="about">关于</string>
</resources>

最后再看一下配置文件:活动都要进行注册,并且设置Activity03为主活动

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="mars.activity03"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity03"
                  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=".ResultActivity" android:label="@string/resultLabel"/><!--这里使ResultActivity标题栏显示result-->
    </application>
    <uses-sdk android:minSdkVersion="4" />

</manifest> 

运行结果:


转载于:https://www.cnblogs.com/wanghang/p/6299687.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package ymq.demo03; import android.app.Activity; import android.os.Bundle; import android.view.*; import android.widget.*; public class demo03 extends Activity { /** Called when the activity is first created. */ String str=""; EditText et; int c=0,flag=0; double b=0.0,g=0.0,f=0.0; View vi; public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, 1, 1, "退出"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if(item.getItemId()==1){finish();} return super.onOptionsItemSelected(item); } //计算方法 public double calculater(){ switch(c){ case 0:f=g;break; case 1:f=b+g;break; case 2:f=b-g;break; case 3:f=b*g;break; case 4:f=b/g;break; } b=f; c=0; return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获得按键 final Button number[]=new Button[10]; final Button fuhao[]=new Button[11]; fuhao[0]=(Button)findViewById(R.id.button01); fuhao[1]=(Button)findViewById(R.id.button02); fuhao[2]=(Button)findViewById(R.id.button03); fuhao[3]=(Button)findViewById(R.id.button04); fuhao[4]=(Button)findViewById(R.id.button05); fuhao[5]=(Button)findViewById(R.id.button06); fuhao[6]=(Button)findViewById(R.id.buttonce); fuhao[7]=(Button)findViewById(R.id.buttonc); fuhao[8]=(Button)findViewById(R.id.zheng); fuhao[9]=(Button)findViewById(R.id.kaifang); fuhao[10]=(Button)findViewById(R.id.pingfang); number[0]=(Button)findViewById(R.id.button0); number[1]=(Button)findViewById(R.id.button1); number[2]=(Button)findViewById(R.id.button2); number[3]=(Button)findViewById(R.id.button3); number[4]=(Button)findViewById(R.id.button4); number[5]=(Button)findViewById(R.id.button5); number[6]=(Button)findViewById(R.id.button6); number[7]=(Butto

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值