当前位置: 移动技术网 > 移动技术>移动开发>Android > Android Studio教程06-布局,监听器以及基本控件

Android Studio教程06-布局,监听器以及基本控件

2019年01月18日  | 移动技术网移动技术  | 我要评论

目录

2. 监听器

  • 一个控件可以设置多个监听器
  • 绑定监听器的步骤
    • 获取代表控件的对象
    • 定义一个类,实现监听器接口
    • 生成监听器对象
    • 为控件绑定监听器对象
public class mainactivity extends appcompatactivity {

    private button bt;
    private textview tv;
    int count=0;
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);

        bt = (button)findviewbyid(r.id.bt1);
        tv = (textview)findviewbyid(r.id.hello);
        //生成监听器对象 new buttonlistener()
        //为控件绑定监听器对象 bt.setonclicklistener
        bt.setonclicklistener(new buttonlistener());

        system.out.println("--mainactivity: oncreate--");
    }

    // 定义一个类,实现监听器接口
    class buttonlistener implements view.onclicklistener{

        @override
        public void onclick(view v) {
            count++;
            tv.settext(count+"");
        }
    }
  }

3. 布局

  • 控件布局方法:就是控制控件在activity中的位置,大小,颜色以及其他控件样式属性的方法
  • 如何设置布局
    • 在布局文件完成控件布局
    • 在java代码中完成控件布局

3.1. 布局分类

(1). linear layout

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <textview
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:text="first text view"/>

    <textview
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ff00"
        android:text=" second text view"/>

</linearlayout>

(2). relative layout

(3). listview

(4). grid view

4. 其他比较杂的内容

4.1. 距离单位的区别px,dp,sp

  • px: 像素分辨率,屏幕是480*800个像素,每个像素可以显示一个rgb颜色

  • dpi:屏幕细腻程度

  • dp:设备无关像素(最主要)

为什么使用dp?

  • sp: 可以缩放的像素:用于指定字体大小

4.2. 控件的外边距和内边距

1. 什么是内外边距

2. 如何设置内外边距

5. android控件

5.1.多选按钮checkbox

1. 如何使用checkbox

private checkbox eatbox, sleppbox, dotabox;
@override
protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.frist_layout);

    eatbox = (checkbox)findviewbyid(r.id.eatid);
    sleppbox = (checkbox)findviewbyid(r.id.sleppid);
    dotabox = (checkbox)findviewbyid(r.id.dotaid);

    onboxclicklistener listener = new onboxclicklistener();
    eatbox.setonclicklistener(listener);
    sleppbox.setonclicklistener(listener);
    dotabox.setonclicklistener(listener);

}

配置文件如下

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <checkbox
        android:id="@+id/eatid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="吃饭"/>

    <checkbox
        android:id="@+id/sleppid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="睡觉"/>

    <checkbox
        android:id="@+id/dotaid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="data"/>

</linearlayout>

2. 常用onclicklisteneroncheckedchangelistener监听器

2.1. onclicklistener监听器

class onboxclicklistener implements view.onclicklistener{
    // view参数是调用setonclicklistener的对象
    // view是checkbox的父类
    // view.getid--查看是哪个对象调用的这个方法
    @override
    public void onclick(view v) {
        // 向下转型
        checkbox box = (checkbox)v;

        if (v.getid() == r.id.eatid){
            system.out.println("eat is clicked");
        }
        else if (v.getid() == r.id.sleppid){
            system.out.println("slepp is clicked");
        }
        else if (v.getid() ==r.id.dotaid){
            system.out.println("dota is clicked");
        }
        // checkbox 是否选中
        if (box.ischecked()){
            system.out.println("clicked");
        }
        else{
            system.out.println("not clicked");
        }


    }
}

2.2. oncheckedchangelistener监听器

compoundbutton

// 选中的时候就会调用这个状态
class checkboxlistener implements compoundbutton.oncheckedchangelistener{

    @override
    public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
        
        if (buttonview.getid() == r.id.eatid){
            system.out.println("eat is clicked");
        }
        else if (buttonview.getid() == r.id.sleppid){
            system.out.println("slepp is clicked");
        }
        else if (buttonview.getid() ==r.id.dotaid){
            system.out.println("dota is clicked");
        }

        // checkbox 是否选中
        if (ischecked){
            system.out.println("clicked");
        }
        else{
            system.out.println("not clicked");
        }

    }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网