本帖最後由 蕭澧邦 於 2019-3-23 14:36 編輯
- <?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: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=".MainActivity"
- android:orientation="vertical"
- android:weightSum="1">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/title"
- android:id="@+id/textView1"
- android:textSize="22sp"
- android:textColor="#000000"
- android:gravity="center"
- android:layout_weight="0.08"
- android:layout_gravity="center_horizontal" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/tv"
- android:id="@+id/textView2"
- android:textSize="22sp"
- android:textColor="#000000"
- android:layout_marginTop="20dp" />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/editText"
- android:textSize="22sp"
- android:inputType="numberDecimal" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="80dp"
- android:id="@+id/textView3"
- android:layout_gravity="center_horizontal"
- android:textSize="22sp"
- android:gravity="center_vertical"
- android:layout_weight="0.08" />
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:gravity="right">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn1"
- android:id="@+id/button1"
- android:textSize="18sp" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn2"
- android:id="@+id/button2"
- android:textSize="18sp" />
- </LinearLayout>
- </LinearLayout>
複製代碼- <resources>
- <string name="app_name">土地面積換算</string>
- <string name="title">1 坪 = 3.3058 平方公尺</string>
- <string name="tv">輸入坪數</string>
- <string name="btn1">清除</string>
- <string name="btn2">換算</string>
- </resources>
複製代碼- package com.example.shou6.myapplication;
- 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 {
- private Button click;
- private Button clean;
- private EditText input;
- private TextView result;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- click = (Button) findViewById(R.id.button2);
- clean = (Button) findViewById(R.id.button1);
- input = (EditText) findViewById(R.id.editText);
- result = (TextView) findViewById(R.id.textView3);
- click.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- double r = input.getText(). * 3.3058;
- result.setText("面積為: " + r + "平方公尺");
- }
- });
- clean.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- input.setText("");
- result.setText("");
- }
- });
- }
- }
- }
複製代碼 |