1、TextView文本框
TextView直接继承了View,并且是EditText、Button的两个UI组件的父类。
作用:在界面上显示文本
详细参数请参考Android Developer
<!--textView 单行,尾部省略,邮箱为超链接-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello world! 1314520@qq.com When a cigarette falls in love with a match,it is destined to be hurt"
android:singleLine="true"
android:ellipsize="end"
android:autoLink="email"
android:id="@+id/textView1"/>
<!--使用drawable资源的TextView-->
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/txt2"
android:text="背景渐变的文字"
android:gravity="center"
android:background="@drawable/bg_border"/>
<?xml version="1.0" encoding="utf-8"?>
<!--文件名为bg_border.xml-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--设置红色边框,宽度2dp-->
<stroke android:width="2dp" android:color="#f00"/>
<!--设置渐变背景色,sweep类型渐变,红-绿-蓝-->
<gradient android:type="sweep"
android:startColor="#f00"
android:centerColor="#0f0"
android:endColor="#00f"/>
<!--指定角度-->
<corners android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"/>
</shape>
2、EditText输入框
EditText与TextView最大的区别是EditText可以接收用户输入
EditText最重要的属性是inputType,用于将EditText设置为指定类型的输入组件。
<!--输入类型为手机号的输入框-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit1"
android:hint="请输入手机号"
android:inputType="phone"/>
EditText 派生了如下两个子类。
- AutoCompleteTextView:需要与Adapter结合使用。
- ExtractEditText:是EditText组件的底层服务类,负责提供全屏输入法支持
3、Button按钮
继承TextView,主要是在UI界面上生成一个按钮,可以提供响应事件(如单击)
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:id="@+id/btn1"/>
4、RadioButton单选框
<!--若单选按钮中默认选中了某个单选按钮,则必须每个按钮都指定android:id 属性,
否则此组单选按钮不能正常工作-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rg_txt"
android:text="性别:"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rg"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/male"
android:checked="true"
android:text="male"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/female"
android:text="female"/>
</RadioGroup>
package com.example.textviewui;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup radioGroup1 = findViewById(R.id.rg);
TextView show = findViewById(R.id.rg_txt);
/*为RadioGroup组件的onCheckedChange时间绑定事件监听器*/
radioGroup1.setOnCheckedChangeListener((radioGroup,checkedId) -> {
String tip = checkedId == R.id.male ? "您的性别是男人" : "您的性别是女人";
show.setText(tip);
});
}
}
5、CheckBox多选框
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/red"
android:text="红色"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/green"
android:text="绿色"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/blue"
android:text="蓝色"/>