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/edit" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:hint="文本输入框" 14 /> 15 </LinearLayout> 16 <LinearLayout 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:layout_gravity="center"> 20 <Button 21 android:id="@+id/bt" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="按钮" 25 /> 26 </LinearLayout> 27 <LinearLayout 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:layout_gravity="center"> 31 <TextView 32 android:id="@+id/txt" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:text="文本标签" 36 /> 37 </LinearLayout> 38 </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 editText=findViewById(R.id.edit); 21 Button button=findViewById(R.id.bt); 22 final TextView textView=findViewById(R.id.txt); 23 button.setOnClickListener(new View.OnClickListener() { 24 @Override 25 public void onClick(View v) { 26 textView.setText(editText.getText().toString()); 27 } 28 }); 29 } 30 }