zoukankan      html  css  js  c++  java
  • Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关

    Android开发 ---基本UI组件2

    1、activity_main.xml 

      描述:

        定义一个按钮

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_main"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"
            android:onClick="test_2"/>
    </LinearLayout>

    2、MainActivity.java

      描述:

        页面跳转

    package com.example.android_ui;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }public void test_2(View view){
            Intent intent=new Intent(this,ButtonActivity.class);
            startActivity(intent);
        }
    }

    3、activity_button.xml

       timg.jpg

      描述:

        1、定义一个图像按钮

           android:src="@drawable/timg"  引用资源文件中的名为timg.jpg的图像

           scaleType=“matrix”            保持原图大小、从左上角的点开始,以矩阵形式绘图。

           scaleType=“fitXY”              将原图进行横方向(即XY方向)的拉伸后绘制的。

           scaleType=“fitStart”           将原图沿左上角的点(即matrix方式绘图开始的点),按比例缩放原图绘制而成的。

           scaleType=“fitCenter”        将原图沿上方居中的点(即matrix方式绘图第一行的居中的点),按比例缩放原图绘制而成的。

           scaleType=“fitEnd”            将原图沿下方居中的点(即matrix方式绘图最后一行的居中的点),按比例缩放原图绘制而成的。

           scaleType=“Center”          保持原图大小,以原图的几何中心点和ImagView的几何中心点为基准,只绘制ImagView大小的图像。

           scaleType=“centerCrop”   不保持原图大小,以原图的几何中心点和ImagView的几何中心点为基准,只绘制ImagView大小的图像(以填满
    ImagView为目标,对原图进行裁剪)。

           scaleType=“centerInside”  不保持原图大小,以原图的几何中心点和ImagView的几何中心点为基准,只绘制ImagView大小的图像(以显示
    完整图片为目标,对原图进行缩放)。

        2、定义一组单选按钮

           RadioGroup组件和RadioButton组件进行结合设置单选按钮

           android:checked="true" 表示默认选中

        3、定义一组多选按钮

           CheckBox组件用来设置多选按钮

        4、定义一个开关

           ToggleButton组件用来设置开关按钮

           android:checked="true" 默认为开

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:id="@+id/activity_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageButton
            android:scaleType="fitXY"
            android:background="@android:color/transparent"
            android:src="@drawable/timg"
            android:onClick="test_click"
            android:layout_width="80dp"
            android:layout_height="80dp" />
    
        <RadioGroup
            android:id="@+id/usexGroup"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/usex1"
                android:text="男"
                android:checked="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <RadioButton
                android:id="@+id/usex2"
                android:text="女"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RadioGroup>
    
        <TextView
            android:text="用户爱好:"
            android:textSize="30sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <CheckBox
                android:id="@+id/ck_1"
                android:text="体育"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <CheckBox
                android:id="@+id/ck_2"
                android:text="娱乐"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <CheckBox
                android:id="@+id/ck_3"
                android:text="睡觉"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <TextView
            android:text="缓存图像"
            android:textSize="30sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <ToggleButton
            android:checked="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    4、ButtonActivity.java

      描述:

        监听按钮选择改变事件

    package com.example.android_ui;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.Toast;
    
    public class ButtonActivity extends Activity{
      //获取多选按钮组件
        private CheckBox ck1,ck2,ck3;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_button);
    
            ck1=(CheckBox)findViewById(R.id.ck_1);
            ck2=(CheckBox)findViewById(R.id.ck_2);
            ck3=(CheckBox)findViewById(R.id.ck_3);
         //给每个多选按钮设置选择改变监听事件,即当该多选按钮被选中了就会被监听到
         //onCheckedChange()方法在下面定义了,即将多选按钮传入到onCheckedChange()方法中,再将这个方法又传给setOnCheckedChangeListener()方法中 ck1.setOnCheckedChangeListener(onCheckedChange(ck1)); ck2.setOnCheckedChangeListener(onCheckedChange(ck2)); ck3.setOnCheckedChangeListener(onCheckedChange(ck3)); }   //定义一个点击图片按钮的事件方法,当点击图片按钮时,弹出Hello
    public void test_click(View view){ Toast.makeText(this, "Hello", Toast.LENGTH_LONG).show(); }   //CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener
      //CompoundButton是一个状态切换控件,它只有两种状态,一种是被选中,一种是未被选中
      //OnCheckedChangeListener是CompoundButton类中定义的一个抽象接口,接口中定义了一个onCheckedChange()方法,这个方法中需要传入一个参数
    public CompoundButton.OnCheckedChangeListener onCheckedChange(final View view){
         //实例化这个接口,并重写onCheckedChanged()方法
    return new CompoundButton.OnCheckedChangeListener() { @Override
           //CompoundButton是一个状态切换控件,它只有两种状态,一种是被选中,一种是未被选中
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
              //将上面传入进来的view强转成多选按钮,并实例化一个多选按钮 CheckBox cb
    =(CheckBox)view;
              //判断传入进来的这个按钮是否被选中
    if(cb.isChecked()) {
                //判断ck1按钮的id是否和多选按钮中的一个id相等,如果相等则将这个相等的按钮的文本内容弹出来
    if (view.getId() == ck1.getId()) {
                  //如果条件成立,则获取被选中按钮的文本内容,并转化为字符串弹出来 Toast.makeText(ButtonActivity.
    this, ck1.getText().toString(), Toast.LENGTH_LONG).show(); } if (view.getId() == ck2.getId()) { Toast.makeText(ButtonActivity.this, ck2.getText().toString(), Toast.LENGTH_LONG).show(); } if (view.getId() == ck3.getId()) { Toast.makeText(ButtonActivity.this, ck3.getText().toString(), Toast.LENGTH_LONG).show(); } } } }; } }
    如果您发现博客内容有什么错误,请您下方留言
  • 相关阅读:
    Java中的final关键字
    使用chrome浏览器调试时的搜索技巧
    查看“文件路径”&“在此处打开命令窗口”
    python安装 错误 “User installations are disabled via policy on the machine”
    Charles 激活入口以及账号密码
    大数据学习(一) | 初识 Hadoop
    Django基础3-数据库交互
    Django基础2
    Django基础使用1
    创建-Django创建启动
  • 原文地址:https://www.cnblogs.com/zn615/p/8195970.html
Copyright © 2011-2022 走看看