学习目标:
成为一个成熟的设计员,成功完成老师作业。输入温度选择转换方向计算得出转化后温度,补充知识点加入个性化图片
学习内容:
- 难点:怎么知道选择了哪个并且将选择结果干预到计算中(尊的好难)
- 布局文件设计(这不轻轻松松)
- 加个小点如何将个性化图片加入设计的页面中
学习产出:
当时纠结的点是 得到一个温度数字 然后又得到一个选择方向 这两个结果都会影响到计算结果双管齐下的监听然后计算应该怎么书写呢?
解决思路 分为两个函数,代码运行时候不是按照书写顺序运行的,是按照内部逻辑。将按钮选择结果赋予一个参数a不同取值,用a的值来作为温度计算方法的flag。真聪明!
来设计一个美美的layout文件吧
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/beijing"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入温度"
android:textSize="30dp"
android:textColor="@color/white"/>
<EditText
android:id="@+id/s1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入一个数字"
android:textSize="25dp"
android:layout_marginLeft="10dp"
android:background="@color/white"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择转换方向"
android:textSize="30dp"
android:layout_marginTop="15dp"
android:textColor="@color/white"/>
<RadioGroup
android:id="@+id/opt"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/opt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="华氏->摄氏"
android:textSize="32dp"
android:textColor="@color/white"/>
<RadioButton
android:id="@+id/opt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="摄氏->华氏"
android:textSize="32dp"
android:textColor="@color/white"/>
</RadioGroup>
<Button
android:id="@+id/btnn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="计算"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/show"
android:textSize="30dp"
android:background="@color/white"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="30dp"/>
</LinearLayout>
欣赏一下
Java
用onchecklistener所以在点击选择之后再点别的不能转换
用onClickListener就可以选择完之后再重新选择
package com.example.badbad;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.EditText;
import android.widget.TextView;
public class temper extends AppCompatActivity {
private int a;
Button btn;
EditText tem;
RadioButton opt1;
RadioButton opt2;
RadioGroup opt;
double exp;
TextView sum,text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chaptertwofour);
tem=(EditText)findViewById(R.id.s1);
sum=(TextView) findViewById(R.id.show);
btn=(Button)findViewById(R.id.btnn);//获得计算按钮
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tem=(EditText) findViewById(R.id.s1);
float str=Float.parseFloat(tem.getText().toString());//不用String用float,不然不能进行运算
if (a==1){
float exp=(float) ((str-32)*5/9);
sum.setText("对应摄氏度温度为:"+exp);
} else if (a==2) {
float exp=(float) (1.8*str+32);
sum.setText("对应摄氏度温度为:"+exp);
}
}
});
init();
}
//
private void init(){
//初始化
opt=(RadioGroup)findViewById(R.id.opt);
text=findViewById(R.id.text);
opt1=(RadioButton) findViewById(R.id.opt1);
opt2=(RadioButton) findViewById(R.id.opt2);//获得按钮
TextView text=findViewById(R.id.text);
opt.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(R.id.radiobtn1==i){
a=1;
}else {
a=2;
}
}});}}
交互部分上一篇文章讲过