<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!--
android:textSize="16sp" 字体大小
-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入用户名"
android:textSize="16sp"
android:textColor="#FFC0CB"
/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名:"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入密码"
android:textSize="16sp"
android:textColor="#FFC0CB"
/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="密码:"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="登录"
android:onClick="click"
/>
</LinearLayout>
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("用户名或密码错误");
}
}
}
结果:
点击后弹出Toast。
BMI指数计算器--
即在一个界面填入数据,跳转到另一个界面进行计算之~
先看主界面布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bmi_bj"
android:paddingLeft="20dp"
android:paddingRight="20dp"
>
<!--
android:paddingLeft="16dp" 页边距 内部显示内容距离边界的距离
android:layout_marginTop="" 与顶部的距离
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="身高(m):"
android:textColor="#00FFFF"
android:textSize="20sp"
/>
<EditText
android:id="@+id/height"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入身高"
android:numeric="decimal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体重(kg):"
android:textColor="#00FFFF"
android:textSize="20sp"
/>
<EditText
android:id="@+id/weight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入体重"
android:numeric="decimal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:layout_marginRight="10dip"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/man"
/>
<ImageButton
android:layout_marginLeft="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/woman"
/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算"
android:background="@drawable/button_press"
android:textColor="#FFF"
android:layout_marginTop="20dp"
android:onClick="calculator"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清空"
android:background="@drawable/button_press"
android:textColor="#FFF"
android:layout_marginTop="20dp"
android:onClick="clear"
/>
<TextView
android:layout_marginTop="80dp"
android:textColor="#666"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BMI指数(身体质量指数,简称体质指数又称体重指数,英文为Body Mass Index,简称BMI),是用体重公斤数除以身高米数平方得出的数字,是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。主要用于统计用途,当我们需要比较及分析一个人的体重对于不同高度的人所带来的健康影响时,BMI值是一个中立而可靠的指标。"
/>
</LinearLayout>
然后是主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
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/demo1_btn" />
<item android:state_pressed="false" android:state_focused="false"
android:drawable="@drawable/demo2_btn" />
<item android:state_focused="true" android:drawable="@drawable/demo1_btn" />
<item android:state_focused="false" android:drawable="@drawable/demo2_btn" />
</selector>
再看第二个Activity的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bmi_bj"
android:gravity="center_horizontal"
>
<!--
android:gravity="" 布局内部组建的对其方式
center_horizontal 水平剧终
-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试结果"
android:textSize="25sp"
android:layout_marginTop="20dp"
/>
<!--
android:src="@drawable/demo1_4"
-->
<ImageView
android:layout_marginTop="20dp"
android:layout_width="150dip"
android:layout_height="300dip"
android:id="@+id/myImageView"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的BMI指数为:"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666"
android:id="@+id/myBMI"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的体重情况为:"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/weightcondition"
android:textColor="#666"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的健康体重为:"
/>
<TextView
android:id="@+id/health_weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#F00"
/>
</LinearLayout>
</LinearLayout>
第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();
}
}
运行效果