Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关

时间:2023-02-24 18:35:14

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

Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关

Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关

  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();
}
}
}
};
}
}

Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关的更多相关文章

  1. Android开发 ---基本UI组件8:九宫格布局、setOnItemClickListener&lpar;&rpar;项被选中监听事件

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?&gt ...

  2. Android开发 ---基本UI组件3:单选按钮、多选按钮、下拉列表、提交按钮、重置按钮、取消按钮

    Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个用户注册按钮 <?xml version="1.0" encoding=&q ...

  3. Android开发 ---基本UI组件4:拖动事件、评分进度条、圆圈式进度条、进度条控制

    Android开发 ---基本UI组件4 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding=&quot ...

  4. Android开发 ---基本UI组件5:监听下拉选项,动态绑定下拉选项、全选&sol;反选,取多选按钮的值,长按事件,长按删除,适配器的使用,提示查询数据,activity控制多按钮

    效果图: 效果描述: 1.当点击 1 按钮后,进入选择城市的页面,会监听到你选中的城市名称:动态为Spinner绑定数据 2.当点击 2 按钮后,进入自动查询数据页面,只要输入首字母,就会动态查找以该 ...

  5. Android开发 ---基本UI组件6 :只定义一个listView组件,然后通过BaseAdapter适配器根据数据的多少自行添加多个ListView显示数据

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?&gt ...

  6. Android开发 ---基本UI组件7 :分页功能、适配器、滚动条监听事件

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?&gt ...

  7. Android开发---基本UI组件1:自动拨电话,自动上网,输入框不换行、只输数字、只输文本、只输密码

    1.activity_main.xml 描述:构建一个按钮 <?xml version="1.0" encoding="utf-8"?> <L ...

  8. android开发之自定义组件

    android开发之自定义组件 一:自定义组件: 我认为,自定义组件就是android给我们提供的的一个空白的可以编辑的图片,它帮助我们实现的我们想要的界面,也就是通过自定义组件我们可以把我们要登入的 ...

  9. ReactNative Android之原生UI组件动态addView不显示问题解决

    ReactNative Android之原生UI组件动态addView不显示问题解决 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com ...

随机推荐

  1. Proximal Gradient Descent for L1 Regularization

    [本文链接:http://www.cnblogs.com/breezedeus/p/3426757.html,转载请注明出处] 假设我们要求解以下的最小化问题:                     ...

  2. HD 1533 Going Home(最小费用最大流模板)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. 四、saltstack如何管理对象?

    实验前环境: [root@super65 ~]# salt-key -L[root@super65 ~]# salt-key -a super65.cn -y salt管理对象简介: saltstac ...

  4. R编程感悟

    虽然大学阶段曾经学过C++, matlab等编程,但是真的几乎完全还给了老师- 所以,我一直将R 作为自己真正学习的第一门语言.我从2012年初在来美国的第二个rotation中开始接触到了R.当时不 ...

  5. eclipse mybatis Generator

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. &lbrack;转&rsqb; Makefile的条件执行

    条件语句可以根据一个变量的值来控制make执行或者忽略Makefile的特定部分.条件语句可以是两个不同变量.或者变量和常量值的比较.要注意的是:条件语句只能用于控制make实际执行的makefile ...

  7. openCV(一)---将openCV框架导入iOS工程中

    开发环境: Xcode 6.4   openCV for iOS 3.0    配置openCV开发环境 在OpenCV官网中下载OpenCV开发包(官网地址:http://opencv.org/) ...

  8. JVM GC知识回顾

    这两天刚好有朋友问到我面试中GC相关问题应该怎么答,作为java面试中热门问题,其实没有什么标准回答.这篇文章结合自己之前的总结,对GC做一个回顾. 1.分代收集 当前主流VM垃圾收集都采用&quot ...

  9. EhCache 在集群环境中使用缓存系统

    EhCache 分布式缓存/缓存集群  EhCache提供了很多种解决方案 这里只介绍一种最常用而且简单的RMI方式分布式缓存决绝方案 Automatic Peer Discovery 自动成员发现方 ...

  10. C&num; QQ邮箱授权码发送邮件

    using System.Net;using System.Web.Mail; public class SendMail { /// <summary> /// 发送Email /// ...