策略、工廠模式融合 InitializingBean
阿新 • • 發佈:2020-10-13
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#9F9F9F" android:editable="true" android:gravity="center" android:text="計算器" android:textSize="25sp" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="true" android:text="輸入第一個數" /> <EditText android:id="@+id/nu1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:numeric="integer"/> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="true" android:text="輸入第二個數" /> <EditText android:id="@+id/nu2" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:numeric="integer" /> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/sum" android:layout_width="152dp" android:layout_height="wrap_content" android:editable="false" android:text="+" /> <Button android:id="@+id/reduce" android:layout_width="152dp" android:layout_height="wrap_content" android:text="-" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/ride" android:layout_width="152dp" android:layout_height="wrap_content" android:text="x" /> <Button android:id="@+id/except" android:layout_width="152dp" android:layout_height="wrap_content" android:text="/" /> </LinearLayout> <TextView android:id="@+id/textView3" android:layout_width="61dp" android:layout_height="38dp" android:editable="true" android:text="結果" /> <TextView android:id="@+id/end" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" /> </LinearLayout>
package com.example.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 implements View.OnClickListener { private int i=0; private int n1; private int n2; private TextView t; private Button sum,re,ride,except; private EditText num1,num2; private TextView t2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); num1 = (EditText)findViewById(R.id.nu1); num2 = (EditText)findViewById(R.id.nu2); t2 = (TextView)findViewById(R.id.end); sum = (Button)findViewById(R.id.sum); re = (Button)findViewById(R.id.reduce); ride = (Button)findViewById(R.id.ride); except = (Button)findViewById(R.id.except); sum.setOnClickListener(this); re.setOnClickListener(this); ride.setOnClickListener(this); except.setOnClickListener(this); } @Override public void onClick(View v){ n1=Integer.parseInt(num1.getText().toString()); n2=Integer.parseInt(num2.getText().toString()); switch(v.getId()){ case R.id.sum: i = n1+n2; break; case R.id.reduce: i = n1-n2; break; case R.id.ride: i = n1*n2; break; case R.id.except: i = n1/n2; break; } String a = String.valueOf(i); t2.setText(a); } }