`
XiangdongLee
  • 浏览: 87262 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

【攻克Android (6)】事件

阅读更多
本文围绕以下两个部分展开:

一、“办公自动化”案例
二、“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);  
     }  
 }
  • 大小: 15 KB
  • 大小: 22.2 KB
0
0
分享到:
评论

相关推荐

    android Activity生命周期 详解

    详尽解释 android 里的Activity的生命周期问题,攻克开发android程序中的难题

    Android程序技术:开拓创新.pptx

    Android 程序技术 本节课程内容:开拓创新 开拓创新 开拓创新 Blaze new trails in a pioneering spirit 开拓创新 Blaze new trails in a pioneering spirit 创新区别于发明 发明是从无到有,而创新是除旧创新。从...

    Android专项测试之GPU测试探索

    这一方面是由于系统没有提供相关接口与命令,另一方面似乎业界目前对于GPU的关注度不足,相关积累与沉淀较少,鉴于此,个人感觉GPU测试这一块也可以作为终端专项后面需要关注及攻克的课题。通过这两天的调研,笔者将...

    MySQLite 文档

    android MySQLiteT的学习文档,源码,迅速攻克MySQLite

    Android高级UI特效仿直播点赞动画效果

    攻克难点: 心形图片的路径等走向 心形图片的控制范围 部分代码如下: 通过AbstractPathAnimator定义飘心动画控制器 @Override public void start(final View child, final ViewGroup parent) { parent.addView...

    linux系统中身份证和二代三代社保卡阅读范例

    此身份证阅读器Linux系统SDK是最新的采用USB接口通讯的开发包,涵盖了身份证、M1、CPU、IC等多种卡片的Linux系统应用示例,包括了Linux系统上面身份证相片解码(已攻克Linux相片解码难题)。 目前测试版本:X86构架...

    attaf-riski

    我是阿塔夫 :laptop: 关于我 :telescope: 我目前正在学习Flutter应用程序开发 :thinking_face: 探索新技术,开发软件解决方案和快速攻克。 :graduation_cap: 学习计算机科学,计算机程序设计和数学。 :briefcase: ...

    零基础学Java,通俗易懂的Java入门课

    同时,在微服务、云计算、大数据、Android App 开发等领域,Java 也是当之无愧的主角。可以说,学好了 Java,不愁没有用武之地,未来的就业之路也会非常宽广。考虑到有不少人想学习编程,但苦于没有合适的教材,或者...

    leetcode安卓-leetcode:leetcode练习题收集,渐渐锻炼自己的算法思维能力

    两年的时间里,加班的日子数都数不过来,直到2019-07-09日,突发奇想,上leetcode做做算法,锻炼一下自己一固搬砖的思维,结果发现自己菜得不行,因此,开启了算法练习之路,希望将来能提升自己,攻克步步难关。...

    智能手机的主要功能都有哪些.docx

    6、帮助科研人员攻克癌症等顽疾 智能手机的主要功能都有哪些全文共3页,当前为第3页。智能手机的主要功能都有哪些全文共3页,当前为第3页。在人类与癌症和其他顽疾进行的斗争中,智能手机也能出一份力此外。 智能...

    智能手机的主要功能都有哪些.pdf

    6、帮助科研⼈员攻克癌症等顽疾 在⼈类与癌症和其他顽疾进⾏的⽃争中,智能⼿机也能出⼀份⼒此外。 7、对汽车进⾏诊断 它被⽤来监测车辆状况以及诊断可能出现的故障。借助转接器和专门的应⽤,智能⼿机也能作为实 ...

Global site tag (gtag.js) - Google Analytics