首页 > 技术文章 > Android Study-week1温度转换器

jvkal 2020-03-05 17:26 原文

Android 学习记录

温度转换器APP制作

1. 调整修改布局代码

  • activity_main.xml

    • 修改文件原来的代码第一步
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>
    </LinearLayout>
    
  • activity_main.xml

    • 对这个文件进行进一步的修改
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:layout_gravity="center"
            android:textAlignment="center"
            android:textSize="32sp"/>
    </LinearLayout>
    
    
    • 首先TextView表示的是文本框

    • width设置为match_parent让这个文本框设置为父类的宽度

    • gravity 设置控件的上下位置居中

    • textAlignment 设置文字居中

    • textSize设置文字大小

运行演示效果

在这里插入图片描述

接下来继续完成我们的温度计

2. 添加界面控件、编辑相关资源

最后的完成效果

在这里插入图片描述

(1)添加界面控件

​ 这里可以手动拖拽,但是作为程序员应该先理解基本的代码,所以下面将会给你演示如何通过写xml来完成这个项目

  • activity_main.xml

    还是修改这个文件,这个文件是对视图进行修改

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
            android:orientation="vertical"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:gravity="center_horizontal"
            android:text="Hello World!"
            android:textAlignment="center"
            android:textSize="32sp" />
    
        <TextView
            android:id="@+id/tv_result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="100dp"
            android:gravity="center_horizontal"
            android:text="Hello World!"
            android:textAlignment="center"
            android:textSize="64sp"
            android:textStyle="bold" />
        <EditText
            android:id="@+id/et_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:textAlignment="center"
            android:textSize="24sp"
            android:gravity="center_horizontal"
            android:inputType="numberDecimel"
            android:textStyle="bold"
            android:hint="请输入温度值"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_margin="20dp">
            <Button
                android:id="@+id/btn_c2f"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_margin="10dp"
                android:textSize="20sp"
                android:text="摄氏转华氏"/>
            <Button
                android:id="@+id/btn_f2c"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_margin="10dp"
                android:textSize="20sp"
                android:text="华氏转摄氏"/>
        </LinearLayout>
    </LinearLayout>
    
    

其中几点进行解释:

  • android:orientation="horizontal" 表示水平分布
  • android:orientation="vertical" 表示竖直排列的
  • layout_weight表示占比权重

然后为了让我们界面中的文字修改方便,所以我们将文字储存到字符串中去,如果修改直接修改这个文件就可以了

找到string.xml文件

(2).编辑相关资源

  • string.xml
<resources>
    <string name="app_name">Week1_task</string>
    <string name="value_hint">请输入温度值</string>
    <string name="celsius_to_fahren">摄氏转华氏</string>
    <string name="fahren_to_celsius">华氏转摄氏</string>
    <string name="msg_error_input">请输入有效温度值</string>
    <string name="fahren">华氏</string>
    <string name="celsius">摄氏</string>
    <string name="to_fahren">度->华氏温度</string>
    <string name="to_celsius">度->摄氏温度</string>
</resources>

对原来的activity_main.xml进行修改

  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:gravity="center_horizontal"
        android:textAlignment="center"
        android:textSize="32sp" />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="100dp"
        android:gravity="center_horizontal"
        android:textAlignment="center"
        android:textSize="64sp"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/value_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:textAlignment="center"
        android:textSize="24sp"
        android:gravity="center_horizontal"
        android:inputType="numberDecimel"
        android:textStyle="bold"
        android:hint="请输入温度值"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="20dp">
        <Button
            android:id="@+id/celsius_to_fahren"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:textSize="20sp"
            android:text="摄氏转华氏"/>
        <Button
            android:id="@+id/fahren_to_celsius"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:textSize="20sp"
            ndroid:text="华氏转摄氏"/>
    </LinearLayout>
</LinearLayout>

3.编写java代码实现功能

  • MainActivity.java
package com.ncu.week1_task;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView tvTitle;       //静态文本框
    private TextView tvResult;
    private EditText editInput;     //定义文本框
    private Button btnC2F;          //定义按钮
    private Button btnF2C;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //通过finViewById方法实例化以上控件类
        tvTitle = (TextView)findViewById(R.id.tv_title);
        tvResult = (TextView)findViewById(R.id.tv_result);
        editInput = (EditText) findViewById(R.id.et_input);
        btnC2F = (Button)findViewById(R.id.celsius_to_fahren);
        btnF2C = (Button)findViewById(R.id.fahren_to_celsius);

        //设置按钮点击监听
        btnC2F.setOnClickListener(this);
        btnF2C.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.celsius_to_fahren:
                outputValue(false);
                break;
            case R.id.fahren_to_celsius:
                outputValue(true);
                break;
            default:
        }
    }
    private boolean checkValidInput(){
        if(editInput.getText().length()==0){
            String errorMsg = getResources().getString(R.string.msg_error_input);
            Toast.makeText(this,errorMsg,Toast.LENGTH_LONG).show();
            return false;
        }else{
            return true;
        }
    }
    private void outputValue(boolean isF2C){
        if(checkValidInput()){
            float inputValue = Float.parseFloat(editInput.getText().toString());
            if(isF2C){
                String title = getResources().getString(R.string.fahren);
                title = title + String.valueOf(inputValue);
                title = title + getResources().getString(R.string.celsius);
                tvTitle.setText(title);
                tvResult.setText(String.valueOf(getF2C(inputValue)));
            }else{
                String title = getResources().getString(R.string.celsius);
                title = title + String.valueOf(inputValue);
                title = title + getResources().getString(R.string.fahren);
                tvTitle.setText(title);
                tvResult.setText(String.valueOf(getC2F(inputValue)));
            }
        }
    }
    private float getF2C(float f){
        return ((f-32.0f)/1.8f);
    }
    private float getC2F(float c){
        return (c*1.8f)+32.0f;
    }
}

最后的效果

在这里插入图片描述
在这里插入图片描述
空值输入提示
这里给一下文件的目录结构
在这里插入图片描述

推荐阅读