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

摘要:
˃.RelativeLayout.ListView.GridView4.其他杂项内容4.1.距离单位之间的差异px,dp,sppx:像素分辨率,480*800像素,每个像素可以显示RGB颜色dpi:屏幕精细度dp:设备无关像素(最重要)为什么使用dp?Sp:可缩放像素:用于指定字体大小4.2.控件的外边距和内边距1。什么是内边距和外边距2.如何设置内边距和外边距5.Android控件5.1.多选按钮CheckBox 1.如何使用CheckBoxprivateCheckBoxatbox、sleppbox、,dotabox@OverrideprotectedvotionCreate{super.onCreate;setContentView;eatbox=findViewById;sleppbox=findView ById;dotabox=findViewById;onBoxClickListenerlistener=newonBoxClickListener();eatbox.setOnClickListener;sleppbox.setOnClickListener;dotabox.setOnClickListen;}配置文件如下?

目录
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+"");
        }
    }
  }

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

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

3.1. 布局分类

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

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

(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>

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

(2). Relative Layout

(3). ListView

(4). Grid View

4. 其他比较杂的内容

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

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

  • dpi:屏幕细腻程度
    Android Studio教程06-布局,监听器以及基本控件第5张

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

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

为什么使用dp?

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

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

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

1. 什么是内外边距

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

2. 如何设置内外边距

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

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: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="吃饭"/>

    <CheckBox
        android: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="睡觉"/>

    <CheckBox
        android: 
        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");
        }

    }
}

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

免责声明:文章转载自《Android Studio教程06-布局,监听器以及基本控件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇命名空间与类库关于源代码管理的10 个问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

稳定UI运行结果-自动化测试失败重试和截图

运行自动化测试的时候,有时会因为网络不稳定,测试环境或者第三方环境正在重启而造成用例运行结果不稳定,时而能跑过时而跑不过。这些难以重现的环境因素造成的用例失败会让测试人员很困扰,排查即耗费时间也没有太多改善的空间。针对这种情况,我们可以基于TestNG的重试器和监听器实现失败结果的重试和监听处理,一旦监听到失败的测试用例,启动自己定制的重试方法和处理方法来...

AndroidUI设计之布局-详细解析布局实现

写完博客的总结 : 以前没有弄清楚的概念清晰化 父容器与本容器属性 : android_layout...属性是本容器的属性, 定义在这个布局管理器的LayoutParams内部类中, 每个布局管理器都有一个LayoutParams内部类, android:... 是父容器用来控制子组件的属性. 如android:layout_gravity 是控制组件本...

ZK框架笔记5、事件

        事件是org.zkoss.zk.ui.event.Event类,它通知应用程序发生了什么事情。每一种类型的事件都由一个特定的类来表示。         要响应一个事件,应用程序必须为事件注册一个或更多事件监听器。有3种方式可以为一个组件事件监听器。   (1)一般制定onXXX事件监听器为组件的属性,作为属性定义的事件监听器。 &...

项目中使用Quartz集群分享--转载

原文:http://hot66hot.iteye.com/blog/1726143 在公司分享了Quartz,发布出来,希望大家讨论补充. CRM使用Quartz集群分享 一:CRM对定时任务的依赖与问题 二:什么是quartz,如何使用,集群,优化 三:CRM中quartz与Spring结合使用 1:CRM对定时任务的依赖与问题 1)依赖 (1)每天晚上...

jmeter之如何减负-实现稳定超高并发测试(性能调优)之正确添加监听器

jmeter之如何减负-实现稳定超高并发测试(性能调优)在测试过程中,初学者使用工具不当,添加众多监控组件,非常想看到实时报告,跑不了一会,jmeter就卡死,只得重启 下面来总结下如何正确使用jmeter,有效利用执行资源,小型机器也可以实现高并发负载。 减负一: 优化监听(GUI模式)“查看结果树”,需要勾选“仅日志错误”,这样只会保存错误日志到内存,...

android自定义控件及自定义组合控件

一、构建自定义控件 构建自定义组件 Android中,你的应用程序程序与View类组件有着一种固定的联系,例如按钮(Button)、文本框(TextView),可编辑文本框(EditText),列表框(ListView),复选框(CheckBox),单选框(RadioButton),滚动条(Gallery),微调器(Spinner), 等等,还有一些比较先...