Android03——TextView和EditText的简单学习

版权声明:为中华之崛起而努力! https://blog.csdn.net/Kj_Gym/article/details/82502182

TextView

一、在Android系统中,任何可视化控件都是继承自android.view.View类
    任何从android.view.View继承的类都被称为视图。
    开发人员有两种方式来创建视图对象:
        1. 使用xml文件的方式来配置试图相关的属性,然后再装载这些视图
        2. 完全使用java代码的方式来创建视图对象
二、Android SDK中的视图类分为三种
        布局(Layout)
        视图容器(View Container)
        视图类
    android.view.ViewGroup是一个容器类,该类也是View的子类,所有的布局类
        和视图容器都是ViewGroup的子类。
    而视图类直接继承自View类
    
三、TextView:用来呈现文字的显示
    系统中常用的属性通过android:来引用是因为有xmlns的命名空间的定义
    (一)常用属性:
            text:文本显示
            textColor:文字颜色
            textSize:文字大小
            
            visibility:可见性
                默认是可见
                invisible:不可见,但是控件的显示区域仍保留
                gone:隐藏控件,并且当前控件的显示区域不保留
            
            maxLength:文本长度
            maxLines:文本行数
            
            autoLink:自动链接,表示当前textView的链接类型
                web:网页
    (二)一个跑马灯的效果:
        

<TextView
            android:text="深圳的发展和经验表明,我们建立经济特区的政策是正确的深圳的发展和经验表明,我们建立经济特区的政策是正确的深圳的发展和经验表明,我们建立经济特区的政策是正确的"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:textSize="20sp"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:scrollHorizontally="true"
             />


    (三)给文字周围添加图片
            直接background天界图片会将文字覆盖,不是预想的结果
            可以考虑drawableTop/left/right/bottom

    EditText

一、用于输入文本的一个控件。
        text:文本显示
        textSize:文字大小
        textColor:文字颜色
        visibility:可见性。。。对所有的控件都适用
        ems:默认EditText的字符呈现的宽度。
        inputType:输入类型。number,textPassword。。。。
        除此之外,EditText继承自TextView,所以TextView的属性对EditText都适用。
        
        明密文切换效果:这里取名字为EditText editText
            editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            editText.setTransformationMethod(passwordTransformationMethod.getInstance());
            
            需要注意的是明密文转换后会将光标的位置移动到首位,需要加一行代码将光标移动到最后。
            editText.setSelection(editText.getText().length());
        
            设置光标不可见:editText.setCursorVisible(false);
        
        添加内容发生变化的监听:TextWatcher
            该监听中回调
                onTextChanged
                beforeTextChanged
                afterTextChanged

二、一个示例

activity_main.xml

<?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:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入。。"
        android:inputType="textPassword"
        android:singleLine="true"/>

</LinearLayout>

MainActivity.java

package com.kjgym.demo01;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化
        editText = findViewById(R.id.editText);

        // 添加监听
        editText.addTextChangedListener(new TextWatcher() {
            /**
             *
             * @param s:表示改变之前的内容,通常start和count组合,可以在s中读取本次改变字段中被改变的内容,
             *         而after表示改变后新的内容的数量
             * @param start:开始的位置
             * @param count:被改变的缘由的内容的个数
             * @param after:改变之后的内容的数量
             */
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("TAG","前");
            }

            /**
             * 文本发生变化的时候执行的方法
             * @param s:
             * @param start
             * @param before
             * @param count:表示新增加的数量
             */
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("TAG","ing");
            }

            /**
             *
             * @param s:表示最终的内容
             */
            @Override
            public void afterTextChanged(Editable s) {
                Log.i("TAG","后");
            }
        });

    }
}

猜你喜欢

转载自blog.csdn.net/Kj_Gym/article/details/82502182