Android---05---简单登录demo与BMI指数计算器



    
    
    
   
   



package com.example.test_login;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText name;
	private EditText password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		name = (EditText) findViewById(R.id.name);
		password = (EditText) findViewById(R.id.password);
	}

	public void click(View view) {
		 
         if(name.getText().toString().equals("abc")&&password.getText().toString().equals("abc")) 
         {   
        	 //1.context 
        	 //MainActivity.this 当前Activity对象
        	 //由于Toast在当前Activity显示,所以设置.this
        	 //2.res
        	 //Toast 显示的文字
        	 //3.duration  持续的时间
        	 Toast.makeText(MainActivity.this, "跳转", Toast.LENGTH_SHORT).show();
        	 //show() 显示Toast
         }
         else
         {
        	 name.setError("用户名或密码错误");
        	 password.setError("用户名或密码错误");
         }
	}

}


结果:

Android---05---简单登录demo与BMI指数计算器_第1张图片Android---05---简单登录demo与BMI指数计算器_第2张图片




点击后弹出Toast。







BMI指数计算器--

即在一个界面填入数据,跳转到另一个界面进行计算之~

先看主界面布局:




    
  

      
      
    


  

      
      
    



  
      
      
     
         
             
         
  
  


  

然后是主Activity


package com.example.test_bmi2;

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

public class MainActivity extends Activity {
	
	
	private TextView userheight;
	private TextView userweight;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		userheight=(TextView)findViewById(R.id.height);
		userweight=(TextView)findViewById(R.id.weight);
	}
   //计算,跳转操作
	public void calculator(View v)
	{ 
		
		String height=userheight.getText().toString();
		String weight=userweight.getText().toString();
		
		if(height.equals(""))
		{
			userheight.setError("请输入数据");
			return;
		}
		else if(weight.equals(""))
		{
			userweight.setError("请输入数据");
			return;
			
		}
		else
		{
		//将字符串转成double
			
	 double  h=	Double.parseDouble(height);
	 double  w=	Double.parseDouble(weight);	
	if(h!=0)
	{
	  double result= w/(h*h);
		//意图 想要做什么操作
		Intent intent=new Intent();
		
		//请求系统打开第二个Activity
		intent.setClass(MainActivity.this, SecondActivity.class);
		//添加传递的数据
		intent.putExtra("bmi", result);
		intent.putExtra("height", h);
		
		//intent.putExtra(name, value);
	    //启动第二个Activity
		
			
		 
		
				startActivity(intent); 
		 

		//}
	 
	}
	else
	{
		userheight.setText("");
	}
		
		
		}
		
	}
	
	public void clear(View v)
	{
		userheight.setText("");
		userweight.setText("");
	}

}


当然,为了让按钮点击有效果我加了个xml



 
 
 
 


再看第二个Activity的布局



     
    









  







  







  





第2个Activity

package com.example.test_bmi2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SecondActivity extends Activity {
   private 	TextView myBMI;
   private  TextView weightCondition;
   private  ImageView img;
   private  TextView  health_weight;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.activity_second);
		myBMI=(TextView)findViewById(R.id.myBMI);
		img=(ImageView)findViewById(R.id.myImageView);
		weightCondition=(TextView)findViewById(R.id.weightcondition);
		health_weight=(TextView)findViewById(R.id.health_weight);
		//1.取出Intent
		Intent  intent=getIntent();
		//2.取出数据
		
		double value=intent.getDoubleExtra("bmi", -1);
	
		
		//四舍五入,保留2位小数
		int v=(int)(value*100+0.5);
		myBMI.setText(v/100.0+"");
		if(value<18.5)
		{
			weightCondition.setText("过轻");	
			//我是用的这个
		//	img.setBackgroundResource(R.drawable.demo1_1);
			//设置图片
			img.setImageResource(R.drawable.demo1_1);
			
		}
		else if(value>=18.5&&value<=24.99)
		{
			weightCondition.setText("正常");		
			img.setImageResource(R.drawable.demo1_2);
			
		}
		else if(value>=20&&value<=25)
		{
			weightCondition.setText("适中");	
			img.setImageResource(R.drawable.demo1_3);
		}
		else if(value>=25&&value<=28)
		{
			weightCondition.setText("过重");		
			img.setImageResource(R.drawable.demo1_4);
		}
		else if(value>=28&&value<=32)
		{
			weightCondition.setText("肥胖");		
			img.setImageResource(R.drawable.demo1_4);
		}
		else if(value>32)
		{
			weightCondition.setText("非常肥胖");	
			img.setImageResource(R.drawable.demo1_4);
		}
		
		else
		{
			img.setImageResource(R.drawable.ic_launcher);
			Toast.makeText(SecondActivity.this,"出错了" , Toast.LENGTH_SHORT).show();
			
		}
		//取出身高数据
		
		//快速复制--->CTRL+SHIFT+箭头
		double height=intent.getDoubleExtra("height", -1);
		double min=(int)((18.5*height*height)*100+0.5)
				/100;
		double max=(int)((23.5*height*height)*100+0.5)
				/100;
		health_weight.setText(min+"~"+max);
		//Toast.makeText(SecondActivity.this,value+"" , Toast.LENGTH_SHORT).show();
	}

	
}



运行效果

Android---05---简单登录demo与BMI指数计算器_第3张图片Android---05---简单登录demo与BMI指数计算器_第4张图片




你可能感兴趣的:(Android基础)