20170122 Radiobutton功能練習 (溫度轉換)
//Radiobutton功能練習 (溫度轉換)
MainActivity.java
package com.example.user.myapplication28;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
implements RadioGroup.OnCheckedChangeListener, TextWatcher {
RadioGroup unit;
EditText value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unit = (RadioGroup) findViewById(R.id.unit);
unit.setOnCheckedChangeListener(this);
value = (EditText) findViewById(R.id.value);
value.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence arg0, int start, int arg1, int arg2) {
}
@Override
public void onTextChanged(CharSequence arg0, int start, int arg1, int arg2) {
}
@Override
public void afterTextChanged(Editable arg0) {
calc();
}
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
calc();
}
protected void calc() {
TextView degF = (TextView) findViewById(R.id.degF);
TextView degC = (TextView) findViewById(R.id.degC);
double f=0,c = 0; //儲存溫度換算結果
if (unit.getCheckedRadioButtonId() == R.id.unitF) {
if(!value.getText().toString().equals("")){ //如果輸入值有輸入時進行以下換算
f = Double.parseDouble(value.getText().toString());
c = (f - 32) * 5 / 9; //華式轉攝氏
}
} else {
if(!value.getText().toString().equals("")) { //如果輸入值有輸入時進行以下換算
c = Double.parseDouble(value.getText().toString());
f = c * 9 / 5 + 32; //攝氏轉華式
}
}
degC.setText(String.format("%.1f", c) + getResources().getString(R.string.charC));
degF.setText(String.format("%.1f", f) + getResources().getString(R.string.charF));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.user.myapplication28.MainActivity"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="200dp"
android:layout_height="58dp"
android:text="@string/TextView"
android:layout_weight="0.76"
android:textColor="#232326"
android:id="@+id/str" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/unit">
<RadioButton
android:text="@string/RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/unitC"
android:layout_weight="1" />
<RadioButton
android:text="@string/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/unitF"
android:layout_weight="1" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned|numberDecimal"
android:ems="10"
android:id="@+id/value"
android:layout_weight="1"
android:maxLength="3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="268dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/degC"
android:layout_weight="1"
android:textSize="24sp"
android:textColor="#000000"
tools:text="\u2103"
android:text="@string/charC" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/degF"
android:layout_weight="1"
android:textColor="#000000"
android:textSize="24sp"
tools:text="\u2109"
android:text="@string/charF" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.user.myapplication28;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
implements RadioGroup.OnCheckedChangeListener, TextWatcher {
RadioGroup unit;
EditText value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unit = (RadioGroup) findViewById(R.id.unit);
unit.setOnCheckedChangeListener(this);
value = (EditText) findViewById(R.id.value);
value.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence arg0, int start, int arg1, int arg2) {
}
@Override
public void onTextChanged(CharSequence arg0, int start, int arg1, int arg2) {
}
@Override
public void afterTextChanged(Editable arg0) {
calc();
}
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
calc();
}
protected void calc() {
TextView degF = (TextView) findViewById(R.id.degF);
TextView degC = (TextView) findViewById(R.id.degC);
double f=0,c = 0; //儲存溫度換算結果
if (unit.getCheckedRadioButtonId() == R.id.unitF) {
if(!value.getText().toString().equals("")){ //如果輸入值有輸入時進行以下換算
f = Double.parseDouble(value.getText().toString());
c = (f - 32) * 5 / 9; //華式轉攝氏
}
} else {
if(!value.getText().toString().equals("")) { //如果輸入值有輸入時進行以下換算
c = Double.parseDouble(value.getText().toString());
f = c * 9 / 5 + 32; //攝氏轉華式
}
}
degC.setText(String.format("%.1f", c) + getResources().getString(R.string.charC));
degF.setText(String.format("%.1f", f) + getResources().getString(R.string.charF));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.user.myapplication28.MainActivity"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="200dp"
android:layout_height="58dp"
android:text="@string/TextView"
android:layout_weight="0.76"
android:textColor="#232326"
android:id="@+id/str" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/unit">
<RadioButton
android:text="@string/RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/unitC"
android:layout_weight="1" />
<RadioButton
android:text="@string/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/unitF"
android:layout_weight="1" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned|numberDecimal"
android:ems="10"
android:id="@+id/value"
android:layout_weight="1"
android:maxLength="3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="268dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/degC"
android:layout_weight="1"
android:textSize="24sp"
android:textColor="#000000"
tools:text="\u2103"
android:text="@string/charC" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/degF"
android:layout_weight="1"
android:textColor="#000000"
android:textSize="24sp"
tools:text="\u2109"
android:text="@string/charF" />
</LinearLayout>
</LinearLayout>
strings.xml
<resources>
<string name="app_name">溫度轉換器 (No28) </string>
<string name="TextView">顯示溫度:</string>
<string name="RadioButton">攝氏</string>
<string name="RadioButton1">華氏</string>
<string name="charC">˚C</string>
<string name="charF">˚F</string>
</resources>
留言
張貼留言