首页 > 其他分享 >android常见界面控件学习

android常见界面控件学习

时间:2023-03-02 17:13:13浏览次数:37  
标签:控件 文本 界面 适配器 点击 android btn id

基本控件

TextView控件

  • layout_width:控件宽度
  • layout_height:控件高度
  • id:唯一标识
  • background:
  • layout_margin:当前控件与屏幕边界或周围控件.布局的距离
  • padding:与控件中内容的距离
  • text:文本内容
  • textColor:
  • textSize:
  • gravity:设置文本位置
  • maxLength:文本最大长度 超过不显示
  • lines:文本行数 超出此行数的文本不显示
  • maxLines:文本最大行数 超出不显示
  • ellipsize:设置当文本超出规定的显示范围时的显示方式
    start middle end 文本超出范围在开始中间或者结尾显示省略号
  • drawableTop:在文本的顶部显示图像
  • lineSpacingExtra:文本的行间距
  • textStyle:文本样式

EditView控件

  • hint:内容为空时候显示的提示文字
  • textColorHint:内容为空的提示文字颜色
  • password: 输入文本框显示.
  • phoneNumber:输入只能是数字
  • minLines:文本最小行数
  • scrollHorizontally 设置若文本超出控件宽度是否出现横拉条
  • editable:是否可编辑 (鼠么意思 可修改?

Button控件

继承自TextView 既可以显示文本 也可以显示图片 还可以点击
设置点击事件 前两种适合点击较少的事件 后一种适合点击较多的事件

  1. 设置onclick属性值
  2. 使用匿名内部类
  3. 使用Activity实现onclickListener接口

在MainActicity中

    private Button btn_one,btn_two,btn_three;

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

        btn_one =findViewById(R.id.btn_one);
        btn_two =findViewById(R.id.btn_two);
        btn_three =findViewById(R.id.btn_three);

        //为按钮3设置点击事件 this指当前实现的onClick的引用
        btn_three.setOnClickListener(this);

        btn_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btn_one.setText("按钮1已被点击");
            }
        });
    }


    public void click(View view){
        //按钮2的点击事件
        btn_two.setText("按钮2已被点击");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_three:
                btn_three.setText("按钮3已被点击");
                break;
        }
    }

在xml文件中

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="8dp"
        tools:ignore="MissingConstraints">

        <Button
            android:id="@+id/btn_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="按钮1"/>

        <Button
            android:id="@+id/btn_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="按钮2"/>

        <Button
            android:id="@+id/btn_three"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="按钮3" />

    </LinearLayout>

效果如图 此时哪个按钮被点击则文字被设置为按钮x已被点击
image

imageView控件

继承自view控件

  • layout_width;layout_height;id;backgroud;layout_margin
  • src:需要显示的图片资源
  • scaleType:将图片资源缩放或者移动,以适应imageView控件的宽高
  • tint:将图片渲染成指定的颜色
  • src和bg的区别在于 bg设置的是背景 是控件大小,而src是前景 按照原图大小
    个人认为这两最大的区别在于bg会将图片进行拉伸,而src不管怎么设置大小 图片都是原始的尺寸,如果设置的尺寸不匹配就空着

图中背景bg会拉伸 对于src的宽高设置不一致 但是还是以原图的尺寸呈现
image

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/image" />

        <ImageView
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:src="@drawable/image" />
</RelativeLayout>

RadioButton控件

单选按钮 是button控件的子类 有选中和未选中两种状态 设置的属性为android:checked
一般来说RadioButton和RadioGroup组合使用,后者是单选组合框,继承于LinearLayout
设置单选组合框

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <RadioGroup
            android:id="@+id/rdg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RadioButton
                android:id="@+id/rbtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="25dp"
                android:text="male"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="25dp"
                android:text="female"/>
        </RadioGroup>
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"/>
    </LinearLayout>

设置RadioGroup的监听事件

	radioGroup = findViewById(R.id.rdg);
        textView = findViewById(R.id.tv);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.rbtn){
                    textView.setText("你的性别是:男");
                }else {
                    textView.setText("你的性别是:女");
                }
            }
        });

效果
image

CheckBox控件

复选框 是Button控件的子类 选中和未选中状态通过checked属性指定
对于复选框也需要设置监听事件 在点击复选按钮的时候 会将当前按钮以及是否checked传入事件函数中,然后进行操作。
image
上图中 复选框中选择什么就会显示什么
监听事件实现如下:当点击某个按钮之后 获得他的text,当这个兴趣爱好被选中时 如果显示的兴趣爱好中未包含它,则加上它之后显示;当未被选中时,如果显示的兴趣爱好包含它,则取消它之后显示。

public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String motion = compoundButton.getText().toString();
        if (b){
            if (!hobbys.contains(motion)){
                hobbys = hobbys+motion;
                hobby.setText(hobbys);
            }
        }else {
            if (hobbys.contains(motion)){
                hobbys = hobbys.replace(motion,"");
                hobby.setText(hobbys);
            }
        }
}

Toast类

轻量级信息提醒机制 向用户提示即时消息,显示在最上层 一段时间后自动消失不打扰当前操作 不获得焦点。
Toast.makeText(Context,Text,Time).show()

列表控件

ListView

  • listSelector 当条目被点击后 改变条目的背景颜色
  • divider 设置分割线的颜色
  • dividerHeight 设置分割线高度
  • scrollbars 是否显示滚动条
  • fadingEdge 去掉上边和下边的黑色阴影

数据适配器

是什么:数据适配器是数据和视图之间的桥梁 类似于一个转换器 将复杂的数据转换成用户可以接受的方式呈现

  1. baseAdapter 基本数据适配器
  • getCount :获取item的条数
  • getItem(int position) 获取某个item对象
  • getItemId(int position) 获取某个item的ID
  • getView(int position,View convertView,ViewGroup parant) 获取position对应的item视图,position 时当前item的位置 converView用于复用旧视图,parent用于加载xml布局
  1. SimpleAdapter 继承baseAdapter
  2. ArrayAdapter
    对于如何用数据适配器和listView来实现一个列表,我们举一个购物商城的例子,我们要实现如下界面
    image
    在布局文件中,我们首先有一个listView,我们列表中的所有东西都是在这个listView中的.
<ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

然后我们定义列表中的一个条目,在每一个条目中我们定义了图片,物品的名字,以及实际价格。每一条分割线上下的条目就是一个列表中的实际物品的条目。

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="15dp"
        tools:ignore="MissingConstraints">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="120dp"
            android:layout_height="90dp" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@id/iv"
            android:layout_centerVertical="true"
            >

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="桌子"
                android:textSize="18sp"
                android:textColor="#000000"/>

            <TextView
                android:id="@+id/tv_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="价格:"
                android:textSize="18sp"
                android:layout_marginTop="10dp"
                android:layout_below="@id/title"
                android:textColor="#000000"/>

            <TextView
                android:id="@+id/price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@id/tv_price"
                android:text="1000"
                android:textColor="#000000"
                android:textSize="18sp" />
        </RelativeLayout>

    </RelativeLayout>

然后我们用数据适配器将页面布局和数据结合起来显示,主要有两点,第一点就是我们先找到这个页面的listView这个控件,然后将一个数据适配器绑定在它上面,第二当然就是我们需要实现这个数据适配器,在我们实现这个数据适配器时,我们这里使用了集成baseAdapter,然后对各种方法进行重写,对于getView方法,是我们在显示每一条物品的实际加载方法。

        //绑定listView和数据适配器
        listView = findViewById(R.id.lv);
        MyBaseAdapter adapter = new MyBaseAdapter();
        listView.setAdapter(adapter);

        //实现数据适配器
        class MyBaseAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int i) {
            return titles[i]; //返回条目
        }

        @Override
        public long getItemId(int i) {
            return  i;//返回条目ID
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            //getView方法中的第二个参数View 实际上是划出屏幕的那个item

            //加载一个list_item的页面布局
            View view1 = View.inflate(MainActivity.this,R.layout.list_item,null);
            TextView title = view1.findViewById(R.id.title);
            TextView price = view1.findViewById(R.id.price);
            ImageView iv = view1.findViewById(R.id.iv);
            title.setText(titles[i]);
            price.setText(prices[i]);
            iv.setBackgroundResource(icons[i]);
            return view1;


            //这个地方会不断的销毁和创建item 有时候会非常耗费资源 所以我们可以使用缓存的item来优化一下
        }
    }

RecyclerView控件

跟listView类似,用于列表的显示,而且也需要有数据适配器。
但是它的功能比ListView更加强大,listView智能实现竖直的列表排列,但是它可以有横向或者竖向,实现瀑布流等效果。

自定义控件

留个坑,如果之后有用到再补。

标签:控件,文本,界面,适配器,点击,android,btn,id
From: https://www.cnblogs.com/Dumbo/p/17162413.html

相关文章

  • Android 读写文件
    packagepw.pw11.writetext;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;publicclassMainActivityextendsAppCompatActivity{priv......
  • dev gridControl控件 常用使用方法及设置(实时更新)
    devgridControl控件常用使用方法及设置(实时更新)新增方法:+1.GridControl中GridView的指定列自动排序功能gridView1.BeginSort();//事务开始gridView1.ClearSorting();......
  • Fiddler 对真机(Android 系统)上 App 抓包图文详解 (超全)
    作为测试或开发经常需要抓取手机App的HTTP/HTTPS的数据包,通过查看App发出的HTTP请求和响应数据来协助开发去修复bug。对于测试而言,通过抓包+分析,去定位bug的前后端归属问题......
  • Android自定义view实现加载中、加载失败、无数据
    一、概述Android中经常在有的app中可以见到“加载中”并不是以弹出对话框的形式显示的,而是占用整个屏幕,如果加载失败就会出现加载失败页面,点击加载失败页面中任意区域,都可以......
  • Android面试题汇总
    1.面试题:知道Service吗,它有几种启动方式?Service是一个专门在后台处理长时间任务的Android组件,它没有UI。它有两种启动方式,startService和bindService。startService只是......
  • Android图像处理实例解析
    一、概述本篇文章介绍的是关于Android图像处理相关的,主要有动态修改图像的色相、饱和度及亮度,颜色矩阵,图像像素点调整、图像矩阵实现图像的平移缩放等,Xfermode相关知识点,......
  • Android Studio 友盟api实现apk多渠道打包
    本篇主要给大家介绍利用友盟api实现Android多渠道打包,进入友盟的官网,注册账号,添加对应的应用。1.添加友盟库的依赖2.在manifest.xml中声明appkey,以及渠道占位符3.builde......
  • Android学习之双向滑动菜单
    MainActivity.java: packagecom.example.bidirslidinglayout;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.Vie......
  • Android学习之左侧滑动菜单
    MainActivity.java:packagecom.example.slidinglayout;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnCli......
  • Android从上到下抽屉式效果
    SlidingDrawerDemo.java: packageorg.lee.android;importorg.lee.android.ExpoInterpolator.EasingType;importorg.lee.android.ExpoInterpolator.ExpoInterpolator;imp......