1 <LinearLayout android:layout_width="fill_parent" 2 android:layout_height="fill_parent" 3 android:orientation="vertical" 4 xmlns:android="http://schemas.android.com/apk/res/android" > 5 <LinearLayout 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:layout_gravity="center"> 9 <EditText 10 android:id="@+id/edit1" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:hint="number1" 14 /> 15 <TextView 16 android:id="@+id/txt" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="+" 20 android:textSize="100px" 21 /> 22 <EditText 23 android:id="@+id/edit2" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:hint="number2" 27 /> 28 <Button 29 android:id="@+id/bt" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:text="=" 33 android:textSize="50px" 34 /> 35 <TextView 36 android:id="@+id/txt2" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content"/> 39 </LinearLayout> 40 </LinearLayout>
1 package com.android.myapplication; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.annotation.SuppressLint; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.EditText; 10 import android.widget.TextView; 11 12 public class MainActivity extends AppCompatActivity { 13 14 public String txt; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 final EditText editText1=findViewById(R.id.edit1); 21 final EditText editText2=findViewById(R.id.edit2); 22 final Button button=findViewById(R.id.bt); 23 final TextView textView=findViewById(R.id.txt2); 24 button.setOnClickListener(new View.OnClickListener() { 25 @Override 26 public void onClick(View v) { 27 int a,b; 28 a=Integer.parseInt(editText1.getText().toString());//String通过Interger转换成int 29 b=Integer.parseInt(editText2.getText().toString()); 30 textView.setText(""+(a+b)); //这里很关键 int型需要这样操作:int+"" 31 } 32 }); 33 } 34 }