吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1179|回复: 3
收起左侧

[学习记录] Android studio 学习记录(一)

[复制链接]
aoding 发表于 2022-9-18 16:28

  最近,我有一同学想算算自己的体测成绩,网上虽然有很多类似的计算器,但是,刚好稍微能挤出这么一丢丢的时间吧,我就心血来潮,想自己做一个大学生体测成绩计算器,但是自己对安卓开发纯纯小白一个,除了感兴趣。于是乎,上网找了很多资料、教程,学习相关的知识,真的是眼睛都看花了{:1_923:},弄着弄着发现工作量对我来说有点大了!下面就稍微记录一下我的一些成果吧。(先记录一点,新人第一次发帖,哪里有不对的地方,还望指点,大佬勿喷{:1_893:}

  (一)准备工具

  俗话说“工欲善其事,必先利其器”,找了一大圈,貌似使用Android Studio是一个比较好的选择。但是,这个工具真的是一点也不友好,全是英文,很多都看不懂,需要多摸索摸索,当然了英语好的话除外{:301_998:}。

  (二)主界面设计

  首先嘛,就是创建一个项目,

  

1

1

  这里我们选择一个空项目。
  
  然后,就是为它起一个名字,要用英文,语言选java(很多人都说这是个很实用的语言,但可惜我没学过{:301_1008:})。再下面的这个选我这个就可以了,然后点击“Finishi”。
  

2

2

  接下来,我们就来到了主界面,我们需要双击打开“layout”下面的,目前来说唯一的一个文件,然后注意点击右上角的“code”按钮,显示代码界面。

  

3

3

  接下来就可以开始工作了,先把布局改成线性布局(我只学了这个{:301_1008:}

  

4

4
  这里我们先在res文件夹下面创建一个文件夹,名字自己取就好了,用来存放我们会用到的一些图片资源,直接“复制”、“粘贴”过来就行了。注意,如果你看不到自己新建的文件夹,那就打开“Project”模式
  

5

5
  

6

6

  然后就可以编写我们的代码了。首先,就是将线性布局里面的方向调整为垂直方向。边距我习惯取10dp。
<LinearLayout
    android:layout_width="match_parent"      宽度最大的意思
    android:layout_height="match_parent"     这个是高度最大
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:padding="10dp">
  体测成绩需要的计算的内容还比较多,一共有七项需要计算的,这就需要有对应的文本框,我们一个一个来。1.先给它加一个标题——“体测计算器”,这个用<TextView><TextView                                   
    android:id="@+id/tv_main_1"             记得给他一个id,相当于他的名字以后用的时候方便
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"
    android:text="体测计算器"               文本
    android:textSize="30dp"                 文本大小
    android:gravity="center"                居中放置
    android:textColor="@color/black"/>      文本的颜色2.加入各种输入框(1)BMI的各种框框先用一个水平方向的布局存放身高和体重的输入框(这里有一个小的知识:我用到了“权重”来排列这两个输入框,就是让这两个输入框,能够平分水平方向上的空间,看起来对称。<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

    <EditText
        android:id="@+id/et_main_1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:hint="体重(kg)"
        android:textColorHint="#4A4A4A"
        android:maxLines="1"
        android:inputType="numberDecimal"/>

    <EditText
        android:id="@+id/et_main_2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:hint="身高(m)"
        android:textColorHint="#4A4A4A"
        android:maxLines="1"
        android:inputType="numberDecimal"/>

</LinearLayout>

之后,我们需要将这个,计算的结果显出来,包括计算的数值、成绩、和等级,再用一个水平方向的布局存放输出框,也是用<TextView><LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

    <TextView
        android:id="@+id/tv_main_2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="BMI :"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"/>


    <TextView
        android:id="@+id/tv_main_3"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:maxLines="1"
        android:hint="数值"
        android:gravity="center_horizontal"/>

    <TextView
        android:id="@+id/tv_main_4"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:hint="得分"
        android:gravity="center_horizontal"/>

    <TextView
        android:id="@+id/tv_main_5"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:hint="等级"
        android:gravity="center_horizontal"/>

</LinearLayout>

然后,我们需要一个按钮,用来触发计算这个事件,这个是用<Button>,这个按钮可以放置在最外面的父布局内,因为之后我们还需要有其他的框框<Button
    android:id="@+id/btn_main_1"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:text="计算"
    android:textSize="16sp"
    android:textColor="@color/white"
    android:background="#8BACACAC"
    android:gravity="center"
    android:layout_gravity="center"
    android:layout_marginTop="15dp"/>
这样,我们已经把这个界面部分差不多完成了一部分,当然它还不是那么美观,没关系,接下来,我们先把这个主要的计算程序给写好。首先要声明和找到控件,同时,我们新建一个方法,我给它起个名字叫“function()”,然后我们在这个方法里面找到我们的控件。(这一部分我放在代码里面吧)//声明控件
private EditText weight;//体重
private EditText height; //身高
private TextView result1;//bmi数值
private TextView result2;//bmi得分
private TextView result3;//bmi等级
private Button count;//计算


private void function() {
    // 找到控件
    weight = findViewById(R.id.et_main_1);
    height = findViewById(R.id.et_main_2);
    result1 = findViewById(R.id.tv_main_3);
    result2 = findViewById(R.id.tv_main_4);
    result3 = findViewById(R.id.tv_main_5);
    count = findViewById(R.id.btn_main_1);
    //设置点击事件
    count.setOnClickListener(this);
}
private void count1() {

    //将输入的值转换一下,再去除一下空格,就是这个trim()
    String h1 = weight.getText().toString().trim();
    String w1 = height.getText().toString().trim();
    //判输入的值是否为空
    if (h1.length() == 0 || w1.length() == 0){

        //弹窗代码,弹出提示
        Toast.makeText(this, "你咋不输东西啊!!!", Toast.LENGTH_SHORT).show();
        height.requestFocus();//让当前按钮获取焦点
    }else{


        double h2 = Double.parseDouble(h1);//转化为double类型
        double w2 = Double.parseDouble(w1);
        double r1 =h2/(w2*w2); //用来存储计算好的结果

        //输出数值和成绩的计算
        if (r1 <= 17.1){
            result1.setText(String.valueOf(String.format("%.1f", r1)));//这个实现的是将double类型的r1显示出来并只保留1位小数
            result2.setText(String.valueOf(80));
            result3.setText("低体重");
        }else {
            if (r1 >= 28.0){
                result1.setText(String.valueOf(String.format("%.1f", r1)));
                result2.setText(String.valueOf(60));
                result3.setText("肥胖");
            }else {
                if (r1 <= 23.9){
                    result1.setText(String.valueOf(String.format("%.1f", r1)));
                    result2.setText(String.valueOf(100));
                    result3.setText("正常");
                }else {
                    result1.setText(String.valueOf(String.format("%.1f", r1)));
                    result2.setText(String.valueOf(80));
                    result3.setText("超重");
                }
            }
        }

    }
}

下面,就先来测试一下,嗯,实现了一个功能{:1_908:} ,不堪入目{:1_924:} ,后面的功能也是类似这样的,我还没弄,有时间的话再弄弄。

7

7

免费评分

参与人数 2吾爱币 +1 热心值 +2 收起 理由
KEEPTRYing + 1 我很赞同!
Herculee + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ygiveupc 发表于 2022-9-18 21:18
非常好呀,你在哪里看的教程呀,B站吗
 楼主| aoding 发表于 2022-9-18 21:40
ygiveupc 发表于 2022-9-18 21:18
非常好呀,你在哪里看的教程呀,B站吗

是的呢,不过还有很多是网上查的
yx159247 发表于 2022-9-19 08:21
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 10:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表