初始Android之Activity和Intent(二)

标准体重计算器

实现目标:

如果是男性,标准体重=(身高-80)*0.7

如果是女性,标准体重=(身高-70)*0.6

运行效果图:

关键代码展示:

package cn.bzu.bztc.standardweight;

import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
/*
 * This is a calculation of the standard weight of demo 
 * @date 2016-3-21
 */
public class MainActivity extends Activity
{
	private Button btName;
	private EditText etName;
	private RadioButton rbw,rbm;
	private double standardWeight;//标准体重
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		doEvent();
	}
	/*
	 * 初始化布局
	 */
	private void initView(){
		rbm=(RadioButton)findViewById(R.id.rbm);
		rbw=(RadioButton)findViewById(R.id.rbw);
		btName=(Button)findViewById(R.id.btName);
		etName=(EditText)findViewById(R.id.etName);
	}
	/*
	 * 事件处理
	 */
	public void doEvent(){
		btName.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
			String heighString=etName.getText().toString();
		if (TextUtils.isEmpty(heighString))
		{
			showToast("你的身高未填写");
		}else {
		if (rbm.isChecked()){
			standardWeight = calWeight("man",heighString);
			Intent manIntent =new Intent(MainActivity.this,ShowActivity.class);
			/*
			 * 数据包Bundle,包含性别,身高,体重
			 */
			Bundle bundle=new Bundle();
			bundle.putString("sex","男性");
			bundle.putDouble("standardWeight", standardWeight);
			bundle.putString("height", heighString);
						
			manIntent.putExtra("bundle", bundle);
						
			startActivity(manIntent);
			}else if (rbw.isChecked()) {
			//如果女性的radioButton被选中
			standardWeight=calWeight("woman", heighString);
			Intent womenIntent=new Intent(MainActivity.this,ShowActivity.class);
			/*
			 * 数据包Bundle,包含性别,身高,体重
			 */
			Bundle bundle=new Bundle();
			bundle.putString("sex", "女性");
			bundle.putDouble("standardWeight", standardWeight);
			bundle.putString("height", heighString);
			womenIntent.putExtra("bundle", bundle);
			startActivity(womenIntent);
	}
	{					
	}
	}			
	}
	});
}
	/*
	 * 计算标准体重
	 * @param sex,height
	 * @return standardWeight
	 */
	private double calWeight(String sex,String height){
		if (sex.equals("man"))
		{
			double manWeight=(Integer.parseInt(height)-80)*0.7;
			return manWeight;
		}else if (sex.equals("woman")) {
			double womanWeight=(Integer.parseInt(height)-70)*0.6;
			return womanWeight;
		}
			
		return 1.0;
	}
	
	/*
	 * 显示Toast
	 * @param msg
	 */
	private void showToast(String msg){
		Toast.makeText(this, ""+msg,Toast.LENGTH_LONG).show();
	}


}
页面展示类代码:

package cn.bzu.bztc.standardweight;

import android.R.layout;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
	/*
	 * 展示页面
	 */
public class ShowActivity extends Activity
{	
	private TextView tvShow;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show);
		tvShow=(TextView)findViewById(R.id.tvShow);
		tvShow.setText("你是一位"+getIntent().getBundleExtra("bundle").get("sex")+"\n"+

                                    "你的身高是"+getIntent().getBundleExtra("bundle").get("height")+"厘米"+"\n"+

                                    "你的标准体重是"+getIntent().getBundleExtra("bundle").get("standardWeight")+"公斤"+"\n");
	}
}

总结:1.在RadioButton组件使用过程中,通过查阅资料获得如何选中一个RadioButton,把其放在RadioGroup组件中,在其布局中把RadioGroup属性中设置orientation为horizontal,让其两个RadioButton在方向上水平。

2.Intent是Android中简单的消息传递框架,Intent起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。

信使Intent——打开新的Activity,不传递参数:在一个Activity中可以使用系统提供的StartActivity(Intent intent)方法打开新的Actity,在打开新的Activity前,可以决定是否为新的Activity传递参数:

第一种:打开新的Activity,不传递参数

public class MainActivity extends Activity {

@Override protected void onCreate(Bundle savedInstanceState) {
 Button btnClick=(Button) this.findViewById(R.id.btnClick);
 btnClick.setOnClickListener(new View.OnClickListener(){
         public void onClick(View v) {
          // 新建一个显式意图,第一个参数为当前 Activity 类对象 第二个参数为你要打开的 Activity
          startActivity( new Intent(HelloActivity.this, ShowActivity.class) );
 }});
         }
}

第二种:打开新的Activity,并传递若干个参数给它:

public class MainActivity extends Activity {
  protected void onCreate(Bundle savedInstanceState) {
  btnClick.setOnClickListener(new View.OnClickListener(){ // 点击该按钮会打开一个新的 Activity
           public void onClick(View v) {
                             Intent intent = new Intent(MainActivity.this, ShowActivity.class)
Bundle bundle = new Bundle(); // 该类用作携带数据
bundle.putString( name , 名字");
bundle.putInt("age", 18);
intent.putExtras(bundle); // 附带上额外的数据
startActivity(intent);
 }}); }
}
在新的 Activity 中接收前面 Activity 传递过来的参数:
public class ShowActivity extends Activity {
           protected void onCreate(Bundle savedInstanceState) {
                           Bundle bundle = this.getIntent().getExtras();
                          String name = bundle.getString("name");
                           int age = bundle.getInt("age");
3.为Intent附加数据的两种写法

第一种写法:用于批量添加数据到Intent:

Intent intent=new Intent();

Bundle bundle=new Bundle();//携带数据

bundle.putString("name","名字");

intent.putExtras(bundle);//追加原有的数据

第二种写法:这种写法的作用等价于上面的写法,只不过这种写法是把数据一个个添加到Intent

Intent intent =new Inten();

intent.putExtra("name","名字");

Intent 提供了各类常用类型重载后的putExtra()方法,如:putExtra(String name,String value)、putExtra(String name,long value),在putExtra()方法内部会判断当前Intent对象内部是否已经存在一个Bundle对象,如果不存在就会新建Bundle对象,以后调用putExtra方法传入的值都会存入Bundle对象,下面是Intent的putExtra(String name,String value)方法代码片段:

public class Intent implements Parcelable{
        private Bundle mExtras;
        public Intent putExtra(String name, String value) {       
           if (mExtras == null) {
            mExtras = new Bundle();
        }
        mExtras.putString(name, value);
        return this;


}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值