Android RelativeLayout布局

例题2-8

activity_main.xml代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout android:layout_width="fill_parent"
 3     android:layout_height="fill_parent"
 4     xmlns:android="http://schemas.android.com/apk/res/android">
 5     <TextView
 6         android:id="@+id/label"
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:text="输入一串字符"
10         android:gravity="center"/>
11     <EditText
12         android:id="@+id/txt"
13         android:layout_width="match_parent"
14         android:layout_height="wrap_content"
15         android:layout_below="@id/label"
16         android:hint="在这里输入123"/>
17     <Button
18         android:id="@+id/but1"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:text="重置"
22         android:layout_below="@id/txt"/>
23     <Button
24         android:id="@+id/but2"
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:text="确认"
28         android:layout_toRightOf="@id/but1"
29         android:layout_marginLeft="150dp"
30         android:layout_below="@+id/txt"/>
31     <TextView
32         android:id="@+id/labe2"
33         android:layout_width="match_parent"
34         android:layout_height="wrap_content"
35         android:layout_below="@id/but2"
36         android:gravity="center"/>
37 </RelativeLayout>

Mainactivity代码

 1 package com.example.hello;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.graphics.Color;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10 
11 public class MainActivity extends AppCompatActivity {
12     private TextView txt;
13     private TextView txt2;
14     private Button button1;
15     private Button button2;
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20         txt = (TextView)findViewById(R.id.labe2);
21         txt2 = (TextView)findViewById(R.id.txt);
22         button1 = (Button)findViewById(R.id.but1);
23         button2 = (Button)findViewById(R.id.but2);
24         button1.setOnClickListener(new bClick());
25         button2.setOnClickListener(new mClick());
26     }
27 
28     class mClick implements View.OnClickListener {
29         public void onClick(View view){
30             txt.setText("输入成功");
31         }
32     }
33     class bClick implements View.OnClickListener {
34         public void onClick(View view){
35             txt2.setText("");
36         }
37     }
38 }
原文地址:https://www.cnblogs.com/xiaowangdatie/p/13721139.html