本文围绕以下两个部分展开:
一、“办公自动化”案例
二、“BMI(体重指数)”案例
附 补充代码
一、“办公自动化”案例
实现以下效果:
1. 在 styles.xml(v21) 中,实现状态栏变蓝。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--隐藏标题栏-->
<style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:colorPrimaryDark">@android:color/holo_blue_dark</item>
<item name="android:colorPrimary">@android:color/holo_blue_light</item>
</style>
</resources>
2. 在 strings.xml 中,定义字符串。
<resources>
<string name="app_name">Event</string>
<string name="oa">办公自动化</string>
<string name="phone_hint">请输入手机号</string>
<string name="password_hint">请输入密码</string>
<string name="cb_password">显示密码</string>
<string name="btn_login">登录</string>
<string name="forget_password">忘记密码</string>
<string name="action_settings">Settings</string>
</resources>
3. 在主界面(activity_main.xml) 中,设计布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvOA"
android:text="@string/oa"
android:gravity="center"
android:textSize="30sp"
android:textColor="@android:color/white"
android:background="@android:color/holo_blue_light"
android:layout_width="match_parent"
android:layout_height="150dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvOA"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<EditText
android:id="@+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/phone_hint"
android:inputType="phone"
android:maxLength="11"/>
<EditText
android:id="@+id/txtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtPhone"
android:hint="@string/password_hint"
android:inputType="textPassword"/>
<CheckBox
android:id="@+id/cbPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cb_password"
android:layout_below="@+id/txtPassword"/>
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtPassword"
android:layout_alignParentRight="true"
android:onClick="onClick"
android:text="@string/btn_login"/>
<Button
android:id="@+id/btnForgetPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnLogin"
android:layout_alignParentRight="true"
android:onClick="onClick"
android:text="@string/forget_password"
style="@android:style/Widget.Material.Button.Borderless"/>
</RelativeLayout>
</RelativeLayout>
4. 在主活动(MainActivity) 中,写事件。
package com.xiangdong.event;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
// 1. 声明控件/声明变量 (定义3个即可。输入手机号、输入密码、显示密码)(登录和忘记密码,在代码中写)
private EditText txtPhone;
private EditText txtPassword;
private CheckBox cbPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 2. 初始化控件
txtPhone = (EditText) findViewById(R.id.txtPhone);
txtPassword = (EditText) findViewById(R.id.txtPassword);
cbPassword = (CheckBox) findViewById(R.id.cbPassword);
// 3. 注册 CheckBox 状态改变的事件(选中:显示密码 或 未选中:隐藏密码)
cbPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 事件处理代码
if(isChecked){
// 如果选中则显示密码 (隐藏星号)
txtPassword.setTransformationMethod(
HideReturnsTransformationMethod.getInstance());
} else {
// 否则隐藏密码 (密码转换成星号)
txtPassword.setTransformationMethod(
PasswordTransformationMethod.getInstance()
);
}
}
});
}
// 按钮事件(点击事件)
public void onClick(View view){
//不同的按钮匹配不同的事件。可以用switch,也可以用if-else
switch (view.getId()){
case R.id.btnLogin:
//登录 流程
String phone = txtPhone.getText().toString();
String password = txtPassword.getText().toString();
//格式化输出值 (好处:当值多的时候,不用使用“ ”+的形式)
String text = String.format("Phone:%s\nPassword:%s",phone,password);
Toast.makeText(this,text,Toast.LENGTH_SHORT).show();
break;
case R.id.btnForgetPassword:
//忘记密码 流程
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
二、“BMI(体重指数)”案例
实现以下效果:
1. 在 styles.xml(v21) 中,实现状态栏变蓝。
2. 在 strings.xml 中,定义字符串。
<resources>
<string name="app_name">Bmi</string>
<string name="action_settings">Settings</string>
<string name="bmi">BMI(体重指数)</string>
<string name="male">男</string>
<string name="female">女</string>
<string name="calWeight">计算标准体重</string>
<string name="standWeight">标准体重:</string>
<string name="scope">体重的合理范围:</string>
<string name="weight_hint">输入您的体重(kg)</string>
<string name="height_hint">输入您的身高(cm)</string>
<string name="about">关于</string>
<string name="aboutContent" formatted="false">
世界卫生组织计算标准体重的方法:\n\n
男性:(身高cm - 80) × 70% = 标准体重\n
女性:(身高cm - 70) × 60% = 标准体重\n\n\n
标准体重正负 10% 为正常体重\n
标准体重正负 10% ~ 20% 为体重过重或过轻\n
标准体重正负 20% 以上为肥胖或体重不足
</string>
</resources>
3. 在主界面(activity_main.xml) 中,设计布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvBmi"
android:text="@string/bmi"
android:gravity="center"
android:textSize="30sp"
android:textColor="@android:color/white"
android:background="@android:color/holo_blue_light"
android:layout_width="match_parent"
android:layout_height="100dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvBmi"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<RadioGroup
android:id="@+id/sexGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="@string/male"/>
<RadioButton
android:id="@+id/rbFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/female"/>
</RadioGroup>
<EditText
android:id="@+id/txtHeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sexGroup"
android:hint="@string/height_hint"
android:inputType="number"/>
<EditText
android:id="@+id/txtWeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtHeight"
android:inputType="numberDecimal"
android:hint="@string/weight_hint"/>
<Button
android:id="@+id/btnCalWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtWeight"
android:onClick="onClick"
android:text="@string/calWeight"/>
<Button
android:id="@+id/btnAbout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtWeight"
android:layout_toRightOf="@+id/btnCalWeight"
android:onClick="onClick"
android:text="@string/about"/>
<TextView
android:id="@+id/tvScope"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnCalWeight"
android:text="@string/scope"
android:visibility="gone"/>
<TextView
android:id="@+id/tvAboutContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/aboutContent"
android:visibility="gone"/>
</RelativeLayout>
</RelativeLayout>
4. 在主活动(MainActivity) 中,写事件。
package com.xiangdong.bmi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnCalWeight:
String sheight = this.getEditText(R.id.txtHeight);
String sweight = this.getEditText(R.id.txtWeight);
// 验证 输入的性别/身高 是否有效
if (sheight == null || sheight.trim().length() == 0) {
Toast.makeText(this, R.string.height_hint, Toast.LENGTH_SHORT).show();
return;
} else if (sweight == null || sweight.trim().length() == 0) {
sweight = "0";
}
// System.out.println(sex + " " + sheight + " " + sweight);
int cm = 80;
double cmPer = 0.7;
RadioButton rbFemale = (RadioButton) findViewById(R.id.rbFemale);
if (rbFemale.isChecked()) {
cm = 70;
cmPer = 0.6;
}
int height = Integer.parseInt(sheight);
double weight = Double.parseDouble(sweight);
// 计算标准体重
double standWeight = (height - cm) * cmPer;
double lowWeight = standWeight - standWeight * 0.1;
double hightWeight = standWeight + standWeight * 0.1;
TextView tvScope = (TextView) findViewById(R.id.tvScope);
String tvtext = "标准体重:" + String.valueOf(standWeight) + "\n体重的合理范围:" + String.valueOf(lowWeight) + "~"
+ String.valueOf(hightWeight);
// 弹出体重的提示信息
String text = "";
int color = 0;
if (weight >= lowWeight && weight <= hightWeight) {
text = "亲,是标准体重.继续保持哦...";
color = Color.GREEN;
showMessage(text);
} else if (weight >= (standWeight + standWeight * 0.11)
&& weight <= (standWeight + standWeight * 0.2)) {
text = "亲,体重过重...";
color = Color.YELLOW;
showMessage(text);
} else if (weight <= (standWeight - standWeight * 0.11)
&& weight >= (standWeight - standWeight * 0.20)) {
text = "亲,体重过轻...";
color = Color.YELLOW;
showMessage(text);
} else if (weight > (standWeight + standWeight * 0.2)) {
text = "亲,肥胖...要减肥啦..";
color = Color.RED;
showMessage(text);
} else if (weight < (standWeight - standWeight * 0.2)) {
text = "亲,太瘦...要多吃啦..";
color = Color.RED;
showMessage(text);
}
tvScope.setVisibility(View.VISIBLE);
tvScope.setText(tvtext + "\n" + text);
tvScope.setTextColor(color);
break;
case R.id.btnAbout:
TextView tvAboutContent = (TextView) findViewById(R.id.tvAboutContent);
if (tvAboutContent.getVisibility() == View.VISIBLE) {
// 设置控件隐藏
tvAboutContent.setVisibility(View.GONE);
} else {
// 设置控件可见
tvAboutContent.setVisibility(View.VISIBLE);
}
break;
}
}
/**
* 获得文本框的值
*
* @param id
* @return
*/
private String getEditText(int id) {
return ((EditText) findViewById(id)).getText().toString();
}
/**
* 显示信息
*
* @param message
*/
private void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}