Android 从入门到走后门

Android入门计划<三>---基础控件TextView、Bu

2021-04-26  本文已影响0人  杨阿杨阿阳

布局练习的差不多了,那就来看看控件,然后简单介绍一下几个基础控件,他们所包含的属性,还有实际运用场景。

一、TextView

//android:text 属性,指的是控件的文本内容
  <TextView
        android:id="@+id/tv_1"
        android:text="我是文本框1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
//所对应java代码为  属性所对应的java代码基本上都为:setXXXX 这里用text属性举例, 获取某个属性的值基本为:getXXX
//下面所有部分,默认已经找到控件     
        textView = findViewById(R.id.tv_1);
        textView.setText("我是按钮1");//设置控件文本内容

既然有文字了,那肯定有文字颜色,文字大小,文字样式

 <TextView
        android:id="@+id/tv_1"
        android:text="我是文本框1"
        android:textSize="18sp"
        android:textColor="@color/colorAccent"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
// android:textColor  文字颜色
//android:textSize   文字大小,单位尽量用sp  dp也行得通
//android:textStyle 文字样式,取值:bold(加粗)、italic(斜体)、normal(默认)
//下面展示下分别的效果

加粗:

image.png

斜体:

image.png

默认:(当然如果不写,也就是默认)

image.png

既然是个控件,那么我们也许有需求要改变它的样式,比如 这样的需求


image.png

这里就用到了我们的背景:

//这里为了方便,我先把文本颜色改成白色,为了文章的可读性强一些,接下来只会贴关键性代码

//属性:android:background   可以是背景,可以图片,也可以自定义的
android:background="@color/colorAccent"

先来看看效果:(为了效果明显一点,我把背景改为黑色)


image.png

这里我加上margin和padding属性,为了好看些,margin外边距和padding内边距前面已经说过了哈。这里再贴一下相关代码,实践一下。

//外边距
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:layout_margin="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
//内边距
        android:paddingLeft="10dp"
        android:padding="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"

细心的人可能发现了哈,这个跟上面的图不太一样,角是尖的,不是圆的。 这里再扩展一个写背景的方法。 在目录:app\src\main\res\drawable 中,新建一个资源l文件 并且选为根标签为shape


image.png

然后


image.png
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

</shape>

接下来往里写内容:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--    背景颜色-->
<solid android:color="@color/colorAccent"/>

<!--    边框颜色  边框宽度-->
    <stroke android:color="@color/colorYellow" android:width="4dp"/>

<!--    圆角弧度-->
    <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="4dp"
        android:topLeftRadius="10dp" android:topRightRadius="4dp"/>
</shape>

预览布局如图:


image.png

然后我们放到xml里面看效果

//注意这里的引用方式为 drawable
  android:background="@drawable/textview_bg"
image.png

然后根据自己的需求和样式自己实现就ok了。

然后再补充一个属性叫做

android:visibility
//使用
android:visibility="visible"//取值:visible、gone、invisible
//他们的含义分别是:
//visible:看得见
//gone:找不到了(不在布局中占位)
//invisible:看不见 (会在布局中占位)

下来分别看一下效果


Visible.png
Gone.png
invisible.png

接下来说一下Button

二、Button

说Button之前,我们先来看看它的源码。

//Button的代码量并不多,我们看到它继承于TextView,那么就基本可以知道,TextView的属性它全部可以用。
//至于区别在于,Button在用户触摸的时候会有一个按下去的效果。下图展示
@RemoteView
public class Button extends TextView {
....
}

按钮被触摸前:


image.png

按钮被触摸后:


image.png

哟西,图上不是很明显,可以自己写一下看看。 Button除了默认样式和textview不太一样,别的api和属性都是几乎通用的。这里Button就不说过多了。

三、EditText

yep! 翻译过来就是 编辑文本,像我们使用某信、某Q的时候,我们平时聊天的时候输入文本的地方,就是它了。 我们和上面一样,来瞅瞅它的源码。

public class EditText extends TextView {
...
}

哟西,发现这货也是继承于TextView,那跟button一样咯,api和属性基本通用,不过这里说几个edittext常用到的属性:

        android:hint="提示内容"//默认提示内容,
        android:inputType="textPassword"//输入类型,可以是text、number、password 等等,值很多可以自行根据需求使用
 android:enabled="false"//是否可以编辑



关于inputType的值,罗列一下:

android:inputType=”text”
android:inputType=”textCapCharacters” 字母大写
android:inputType=”textCapWords” 首字母大写
android:inputType=”textCapSentences” 仅第一个字母大写
android:inputType=”textAutoCorrect” 自动完成
android:inputType=”textAutoComplete” 自动完成
android:inputType=”textMultiLine” 多行输入
android:inputType=”textImeMultiLine” 输入法多行(如果支持)
android:inputType=”textNoSuggestions” 不提示
android:inputType=”textUri” 网址
android:inputType=”textEmailAddress” 电子邮件地址
android:inputType=”textEmailSubject” 邮件主题
android:inputType=”textShortMessage” 短讯
android:inputType=”textLongMessage” 长信息
android:inputType=”textPersonName” 人名
android:inputType=”textPostalAddress” 地址
android:inputType=”textPassword” 密码
android:inputType=”textVisiblePassword” 可见密码
android:inputType=”textWebEditText” 作为网页表单的文本
android:inputType=”textFilter” 文本筛选过滤
android:inputType=”textPhonetic” 拼音输入
//数值类型
android:inputType=”number” 数字
android:inputType=”numberSigned” 带符号数字格式
android:inputType=”numberDecimal” 带小数点的浮点格式
android:inputType=”phone” 拨号键盘
android:inputType=”datetime” 时间日期
android:inputType=”date” 日期键盘
android:inputType=”time” 时间键盘

常用的基本告一段落了,有需求的话可以留言或者自行百度。

四、控件的点击

以TextView为例:

textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //这里是点击之后,要执行的事情
                //比如我要让他弹一个吐司
         Toast.makeText(Main2Activity.this,"TextView被点击了",Toast.LENGTH_SHORT).show();
        
            }
        });

看一下效果:

image.png

本节课,结束。 有问题可以评论留言,或者以后做扩展

上一篇下一篇

猜你喜欢

热点阅读