控件TextView与EditText的简单运用

文本框(TextView)
简介
TextView直接继承View,作用就是在界面上显示文本(类似于Swing中的JLabel),同时它还是EditText、Button两个UI组件类的父类。
另外Android关闭了它的文字编辑功能,如果想编辑内容,则可以使用EditText。

编辑框(EditText)
简介
EditText和TextView非常相似,它与TextView共用了绝大总分XML属性和文法,
二者最大区别在于:
1.EditText可以接受用户输入;
2. TextView只能看不能编写。
常用属性
1:inputType:它是EditText组件最重要的属性,它相当于HTML中标签的type属性,用于EditText指定输入组件的类型。
常用取值有:number|numberPassword|date|phone
2: hint:提示字符信息

案例:根据获得/失去焦点边框变色编辑框
选择器
   作用:根据控件状态显示不同样式
  Item: 指定不同状态下控件显示哪个样式

activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/main_edit_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="结果" />

    <EditText
        android:id="@+id/main_edit_result2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="结果2" />

    <Button
        android:layout_width="match_parent"
        android:id="@+id/main_btn_btn1"
        android:onClick="doSubmit"
        android:background="@drawable/main_et_selector"
        android:layout_height="wrap_content" />`这里写代码片`
    <Button
        android:layout_width="match_parent"
        android:id="@+id/main_btn_btn2"
        android:onClick="doSubmit"
        android:layout_height="wrap_content" />
</LinearLayout>
<resources>


MainActivity
package com.basic.t212_a04;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText main_edit_result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_edit_result = findViewById(R.id.main_edit_result);
    }

    public void doSubmit(View view){
        int id = view.getId();
        switch (id){
            case R.id.main_btn_btn1:
                main_edit_result.setText("btn1");
                break;
            case R.id.main_btn_btn2:
                main_edit_result.setText("btn2");
                break;
        }
    }
}

style.xml
<resources>
    <!-- Base application theme. -->
    <style name="AppTheme"  parent="Theme.AppCompat.Light.DarkActionBar">  
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

猜你喜欢

转载自blog.csdn.net/cg_9647/article/details/82598106