【从零到一】Android UI界面(二) | 简单控件篇


  上一篇:Android UI界面(一) | 布局篇

  通过上篇文章对布局的介绍,我们已经对布局的使用有了基本的认识,那么有了布局,哪能没有控件呢?这篇文章就来讲讲Android中常用的简单控件,包括:

  1. TextView(文本框)
  2. EditView(可输入文本框)
  3. AutoCompleteTextView(自动匹配内容的文本框)
  4. MutiAutoCompleteTextView(支持多次自动匹配内容的文本框)
  5. ImageView(图片)
  6. Button(按钮)
  7. ImageButton(图片按钮)
  8. ToggleButton(多状态按钮)
  9. CHeckBox(复选框)
  10. RadioButton(单选按钮)

1. TextView(文本框)

  每种控件都有自己特有的属性,但也有共同的属性。在讲控件之前,我们先来讲下这3个共有属性:

  • android:id:控件的ID,在其他地方可以通过ID找到该控件,格式为android:id="@+id/ 控件ID ";
  • android:layout_width:控件的宽度,值可以是wrap_content(包裹控件的宽)、match_parent(充满父控件的宽)这两个常用值,也可以是自定义的值,单位为dp;
  • android:layout_height:控件的高度,值同 android:layout_width;

  介绍完3个共有属性,可以开始讲 TextView 了,TextView是文本框,用来存放文本信息,我们创建布局时会默认创建一个包含“Hello World!”文本框的布局。
在这里插入图片描述
  “Hello World!”其实是android:text 属性中的内容,我们可以随意更改,接下来我们来看看 TextView 的常用属性:

  • android:text:文本内容,我们可以直接写入文本内容,也可以将具体文本内容放到res/value/strings中,通过 @string/name 来引用(推荐使用后者);
  • android:textSize:文本内容的字体大小,单位为sp;
  • android:textColor:文本内容的字体颜色,可以是 #开头+6位数字,也可以是在res/value/colors中指定的颜色,通过 @color/name 来引用;
  • android:background:控件的背景色,这个属性很多控件也会有,可以用颜色也可以用图片(图片当背景时会被拉伸);

  让我们来看一个综合案例:

    <TextView
        android:id="@+id/Tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textView"//文本内容
        android:textSize="20dp"//字体大小
        android:textColor="#ff8800"//字体颜色:橙色
        android:background="#666666"//背景色:灰色
        //以下是约束布局的一些属性,用来控制控件的位置
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

  这个控件长这样:
在这里插入图片描述


2. EditView(可输入文本框)

  EditView是可输入的文本框,属于特殊的TextView,不仅拥有TextView所有的属性,还有两个特别的属性:

  • android:hint:提示文本内容,用于提示此处应输入什么内容。在用户点击文本框要输入内容时,提示文本内容会消失(android:text 属性的文本内容则不会消失);
  • android:inputType:限定输入内容的类型,保证在特定情况下输入格式的正确性;

  综合案例(在上面的TextView的基础上):

    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="提示文本内容"
        android:inputType="number"
        android:layout_marginTop="28dp"
        app:layout_constraintEnd_toEndOf="@+id/tv"
        app:layout_constraintStart_toStartOf="@+id/tv"
        app:layout_constraintTop_toBottomOf="@+id/tv" />

  这个控件长这样:
在这里插入图片描述


3. AutoCompleteTextView(自动匹配内容的文本框)

  AutoCompleteTextView是特殊的EditView,特殊下哪呢?用过搜索引擎吧,就跟搜索引擎一样,当你输入内容时,会自动提示相关内容,android:completionThreshold 指设置输入多少字符时提示内容,那么提示的内容怎么来的呢?这里我们需要用到一个 适配器(Adapter) 来绑定数据源,再把适配器给到AutoCompleteTextView控件。

  代码如下:

activity_main.xml

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="自动匹配内容文本框"
        android:completionThreshold="2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et" />

MainActivity.java

public class MainActivity extends AppCompatActivity {
   

    private AutoCompleteTextView autoCompleteTextView;//定义autoCompleteTextView对象
    private ArrayAdapter<String> arrayAdapter;//定义适配器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值