zoukankan      html  css  js  c++  java
  • Android-----RadioButton单选使用(实现简单温度转换)

    废话少说,直接上代码:

    xml布局文件代码:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="wrap_content"
     7     android:orientation="vertical"
     8     tools:context="com.hs.example.exampleapplication.RadioButtonMain">
     9 
    10     <LinearLayout
    11         android:layout_width="match_parent"
    12         android:layout_height="wrap_content"
    13         android:orientation="horizontal"
    14         android:gravity="center">
    15 
    16         <TextView
    17             android:layout_width="wrap_content"
    18             android:layout_height="match_parent"
    19             android:gravity="center"
    20             android:text="输入温度:"/>
    21         
    22         <RadioGroup
    23             android:id="@+id/unit"
    24             android:layout_width="wrap_content"
    25             android:layout_height="wrap_content"
    26             android:orientation="horizontal">
    27 
    28             <RadioButton
    29                 android:id="@+id/unitF"
    30                 android:layout_width="wrap_content"
    31                 android:layout_height="wrap_content"
    32                 android:textSize="22sp"
    33                 android:text="华氏"/>
    34 
    35             <RadioButton
    36                 android:id="@+id/unitC"
    37                 android:layout_width="wrap_content"
    38                 android:layout_height="wrap_content"
    39                 android:textSize="22sp"
    40                 android:text="摄氏"/>
    41 
    42             <RadioButton
    43                 android:id="@+id/unitK"
    44                 android:layout_width="wrap_content"
    45                 android:layout_height="wrap_content"
    46                 android:textSize="22sp"
    47                 android:text="绝对"/>
    48 
    49         </RadioGroup>
    50 
    51     </LinearLayout>
    52 
    53     <EditText
    54         android:id="@+id/value"
    55         android:layout_width="match_parent"
    56         android:layout_height="wrap_content"
    57         android:maxLength="9"
    58         android:singleLine="true"
    59         android:inputType="numberDecimal"/>
    60 
    61     <LinearLayout
    62         android:layout_width="match_parent"
    63         android:layout_height="wrap_content"
    64         android:orientation="vertical">
    65 
    66         <TextView
    67             android:id="@+id/degF"
    68             android:layout_width="match_parent"
    69             android:layout_height="wrap_content"
    70             android:layout_weight="1"
    71             android:gravity="center"
    72             android:textSize="45sp"
    73             android:maxLines="1"
    74             android:text="@string/charF"/>
    75 
    76         <TextView
    77             android:id="@+id/degC"
    78             android:layout_width="match_parent"
    79             android:layout_height="wrap_content"
    80             android:layout_weight="1"
    81             android:gravity="center"
    82             android:textSize="45sp"
    83             android:maxLines="1"
    84             android:text="@string/charC"/>
    85 
    86         <TextView
    87             android:id="@+id/degK"
    88             android:layout_width="match_parent"
    89             android:layout_height="wrap_content"
    90             android:layout_weight="1"
    91             android:gravity="center"
    92             android:textSize="45sp"
    93             android:maxLines="1"
    94             android:text="K"/>
    95 
    96     </LinearLayout>
    97 
    98 </LinearLayout>

    Java代码:

     1 public class RadioButtonMain extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,TextWatcher{
     2 
     3     RadioGroup unit;
     4     EditText value;
     5 
     6     @Override
     7     protected void onCreate(Bundle savedInstanceState) {
     8         super.onCreate(savedInstanceState);
     9         setContentView(R.layout.activity_radiobutton_main);
    10 
    11         unit = this.findViewById(R.id.unit);
    12         unit.setOnCheckedChangeListener(this);
    13 
    14         value = this.findViewById(R.id.value);
    15         value.addTextChangedListener(this);
    16 
    17     }
    18 
    19     @Override
    20     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    21 
    22     }
    23 
    24     @Override
    25     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    26 
    27     }
    28 
    29     @Override
    30     public void afterTextChanged(Editable editable) {
    31         calc();
    32     }
    33 
    34     @Override
    35     public void onCheckedChanged(RadioGroup radioGroup, int i) {
    36         calc();
    37     }
    38 
    39     protected void calc(){
    40         TextView degF = this.findViewById(R.id.degF);
    41         TextView degC = this.findViewById(R.id.degC);
    42         TextView degK = this.findViewById(R.id.degK);
    43 
    44         double f,c,k;
    45 
    46         if(unit.getCheckedRadioButtonId() == R.id.unitF){
    47             f = Double.parseDouble(value.getText().toString());
    48             c = (f-32)*5/9;         //华氏度转摄氏度
    49             k = c + 273.15;         //绝对 = 摄氏度 + 273.15
    50 
    51         }else if(unit.getCheckedRadioButtonId() == R.id.unitC){
    52             c = Double.parseDouble(value.getText().toString());
    53             f = c*9/5+32;           //摄氏度转华氏度
    54             k = c + 273.15;
    55 
    56         }else{
    57             k = Double.parseDouble(value.getText().toString());
    58             c = k - 273.15;
    59             f = c*9/5+32;
    60         }
    61 
    62         degC.setText(String.format("%.1f",c)+getResources().getString(R.string.charC));
    63         degF.setText(String.format("%.1f",f)+getResources().getString(R.string.charF));
    64         degK.setText(String.format("%.2f",k)+"K");
    65 
    66     }
    67 
    68 }

    运行效果:

    该例子还存在一些问题,这里就不指出了,运行一遍自会发现问题所在,最好能够自行解决。

  • 相关阅读:
    【转】 java中Class对象详解和类名.class, class.forName(), getClass()区别
    106. Construct Binary Tree from Inorder and Postorder Traversal
    105. Construct Binary Tree from Preorder and Inorder Traversal
    107. Binary Tree Level Order Traversal II
    109. Convert Sorted List to Binary Search Tree
    108. Convert Sorted Array to Binary Search Tree
    110. Balanced Binary Tree
    STL容器迭代器失效问题讨论
    113. Path Sum II
    112. Path Sum
  • 原文地址:https://www.cnblogs.com/xiobai/p/10813927.html
Copyright © 2011-2022 走看看