Android study week1 temperature converter

Android learning history

Temperature converter APP production

1. Adjust and modify layout code

  • activity_main.xml

    • The first step to modify the original code of the file
    <?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

    • Make further changes to this document
    <?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>
    
    
    • First, TextView represents a text box

    • Set width to match [parent] to make this text box the width of the parent class

    • gravity sets the center of the control's up and down position

    • textAlignment set text Center

    • textSize set text size

Run demo effect

Next, let's finish our thermometer

2. Add interface controls and edit related resources

Final finish

(1) Add interface control

You can drag and drop manually here, but as a programmer, you should understand the basic code first, so I will show you how to write xml to complete this project

  • activity_main.xml

    Or modify this file, which is to modify the view

    <?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="Please enter the temperature value"/>
        <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="Celsius to Fahrenheit"/>
            <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="Fahrenheit to Celsius"/>
        </LinearLayout>
    </LinearLayout>
    
    

Several of them are explained:

  • android:orientation = "horizontal" indicates horizontal distribution
  • android:orientation = "vertical" means vertically arranged
  • Layout? Weight means proportion weight

Then in order to facilitate the modification of the text in our interface, we will save the text in the string. If the modification is made, we can modify the file directly

Find the string.xml file

####(2) . edit related resources

  • string.xml
<resources>
    <string name="app_name">Week1_task</string>
    <string name="value_hint">Please enter the temperature value</string>
    <string name="celsius_to_fahren">Celsius to Fahrenheit</string>
    <string name="fahren_to_celsius">Fahrenheit to Celsius</string>
    <string name="msg_error_input">Please enter a valid temperature value</string>
    <string name="fahren">Fahrenheit</string>
    <string name="celsius">C</string>
    <string name="to_fahren">degree->Fahrenheit</string>
    <string name="to_celsius">degree->Celsius temperature</string>
</resources>

Modify the original 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="Please enter the temperature value"/>
    <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="Celsius to Fahrenheit"/>
        <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="Fahrenheit to Celsius"/>
    </LinearLayout>
</LinearLayout>

3. Write java code to realize the function

  • 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;       //Static text box
    private TextView tvResult;
    private EditText editInput;     //Define text box
    private Button btnC2F;          //Define button
    private Button btnF2C;

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

        //Instantiate the above control class through the finViewById method
        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);

        //Set button click monitor
        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;
    }
}

Final effect

[external link image transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-paOc3ZCZ-1583397987170)(D: \ desktop \ Android notes \ Android Study\image-20200305163741404.png))

[external link image transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-gke2aWoL-1583397987172)(D: \ desktop \ Android notes \ Android Study\image-20200305163801998.png))

lt.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;
}
}

Final effect

! [insert picture description here] (https://img-blog.csdnimg.cn/20200305164925639.png? X-oss-process = image / watermark, type_zuz3pozw5nagvpdgk, shadow_, text_z0chm6ly9ibg9nlmnnzzg4ubmv0l3fxzqxindi1mzuz, size_, color_zffff, t_)
! [insert picture description here] (https://img-blog.csdnimg.cn/20200305164931344.png? X-oss-process = image / watermark, type zmfuz3pozw5nagvpdgk, shadow_, text ahr0chm6ly9ibg9nlmnzg4ubmv0l3fxzqxindi1mzuz, size_, color ffffff, t_)
! [if you do not enter a value, you will be prompted] (https://img-blog.csdnimg.cn/20200305164940520. PNG? X-oss-process = image / watermark, type_zfuz3pozw5nagvpdgk, shadow_, text_ahr0chm6ly9ibg9nlmnzg4ubmv0l3fxzqxindi1mzuz, size_, color_ffffff, t_)
Published 12 original articles, won praise 3, visited 469
Private letter follow

Tags: Android xml encoding Java

Posted on Thu, 05 Mar 2020 04:32:20 -0800 by gillms1