<resources> <string name="app_name">CF2</string> <string name="cel_lbl">攝氏:</string> <string name="fah_lbl">華氏:</string> <string name="btn_lbl">轉換成華式</string> </resources>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.iii.lcpan.cf2.MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/cel_lbl" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="numberDecimal" android:ems="10" android:id="@+id/celsius" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_lbl" android:id="@+id/submit" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/fah_lbl" android:id="@+id/fahrenheit" /> </LinearLayout> </RelativeLayout>
package com.iii.lcpan.cf2; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText cel; private TextView fah; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setContentView(View view) Set the activity content to an explicit view. cel=(EditText) findViewById(R.id.celsius); fah=(TextView) findViewById(R.id.fahrenheit); btn=(Button) findViewById(R.id.submit); findViewById(int id) Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle). btn.setOnClickListener(this); //this就是MainActivity物件 } @Override public void onClick(View v) { double f=(Double.parseDouble(cel.getText().toString()))*9/5+32; fah.setText(getText(R.string.fah_lbl)+(Double.valueOf(f).toString())); } }