- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <EditText
- android:id="@+id/editText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:ems="10"
- android:inputType="numberDecimal"
- tools:layout_editor_absoluteX="85dp"
- tools:layout_editor_absoluteY="50dp" />
- <EditText
- android:id="@+id/editText2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="8dp"
- android:ems="10"
- android:inputType="textEmailAddress"
- app:layout_constraintTop_toBottomOf="@+id/editText"
- tools:layout_editor_absoluteX="84dp" />
- <EditText
- android:id="@+id/editText3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="8dp"
- android:ems="10"
- android:inputType="textEmailAddress"
- app:layout_constraintTop_toBottomOf="@+id/editText2"
- tools:layout_editor_absoluteX="84dp" />
- <Button
- android:id="@+id/button"
- android:layout_width="211dp"
- android:layout_height="46dp"
- android:layout_marginTop="8dp"
- android:text="@string/判斷"
- app:layout_constraintTop_toBottomOf="@+id/textView"
- tools:layout_editor_absoluteX="88dp" />
- <Button
- android:id="@+id/button2"
- android:layout_width="207dp"
- android:layout_height="45dp"
- android:layout_marginTop="8dp"
- android:text="@string/清除"
- app:layout_constraintTop_toBottomOf="@+id/button"
- tools:layout_editor_absoluteX="91dp" />
- <TextView
- android:id="@+id/textView"
- android:layout_width="210dp"
- android:layout_height="29dp"
- android:layout_marginTop="8dp"
- app:layout_constraintTop_toBottomOf="@+id/editText3"
- tools:layout_editor_absoluteX="88dp" />
複製代碼- package com.example.plantsai.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 {
- EditText et,et2,et3;
- Button btn,btn2;
- TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- et = findViewById(R.id.editText);
- et2 = findViewById(R.id.editText2);
- et3 = findViewById(R.id.editText3);
- btn = findViewById(R.id.button);
- btn2 = findViewById(R.id.button2);
- tv = findViewById(R.id.textView);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- double a =Double.parseDouble(et.getText()+"");
- double b =Double.parseDouble(et2.getText()+"");
- double c =Double.parseDouble(et3.getText()+"");
- if(a+b>c || b+c>a || a+c>b)
- {
- if(a*a+b*b==c*c || b*b+c*c==a*a || a*a+c*c==b*b)
- {
- tv.setText("直角三角形");
- }else if(a*a+b*b<c*c || b*b+c*c<a*a || a*a+c*c<b*b)
- {
- tv.setText("鈍角三角形");
- }else
- {
- tv.setText("銳角三角形");
- }
- }else
- {
- tv.setText("絕對不是三角形");
- }
- }
- });
- btn2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- et.setText("");
- et2.setText("");
- et3.setText("");
- tv.setText("");
- }
- });
- }
- }
複製代碼 |