<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hanqi.test5"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".UIActivity1">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LongClickActivityActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity android:name=".CalculatorActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity1" /> </application> </manifest>

当然每一次都少不了先注册,这个是绝对不能忘的,第二步就是写java代码了

package com.hanqi.test5;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton; public class UIActivity1 extends AppCompatActivity { ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ui1);
//单选框
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rg); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
if (checkedId == R.id.rb3) //rb3设定为正确答案
{
Toast.makeText(UIActivity1.this, "选对了", Toast.LENGTH_LONG).show();
}
RadioButton rb = (RadioButton) findViewById(checkedId);
Toast.makeText(UIActivity1.this, rb.getText(), Toast.LENGTH_LONG).show();
}
}); //复选框
CheckBox cb_st = (CheckBox)findViewById(R.id.cb_st);
cb_st.setOnCheckedChangeListener(new CBOnCheckedChangListenter() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { }
}); cb_st.setOnCheckedChangeListener(new CBOnCheckedChangListenter()); CheckBox cb_jc = (CheckBox)findViewById(R.id.cb_jc);
cb_jc.setOnCheckedChangeListener(new CBOnCheckedChangListenter()); CheckBox cb_xt = (CheckBox)findViewById(R.id.cb_xt);
cb_xt.setOnCheckedChangeListener(new CBOnCheckedChangListenter()); CheckBox cb_xhx = (CheckBox)findViewById(R.id.cb_xhx);
cb_xhx.setOnCheckedChangeListener(new CBOnCheckedChangListenter()); iv =(ImageView)findViewById(R.id.iv); ToggleButton tob =(ToggleButton)findViewById(R.id.tob);
tob.setOnCheckedChangeListener(new TOnCheckedChangeListenter());
Switch sw =(Switch)findViewById(R.id.sw);
sw.setOnCheckedChangeListener(new TOnCheckedChangeListenter());
}
private class TOnCheckedChangeListenter implements CompoundButton.OnCheckedChangeListener{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
iv.setImageResource(R.drawable.on);
}
else
{
iv.setImageResource(R.drawable.off);
}
}
} private class CBOnCheckedChangListenter implements CompoundButton.OnCheckedChangeListener
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CheckBox cb = (CheckBox)buttonView; if (isChecked) {
Toast.makeText(UIActivity1.this, "选中了" + cb.getText(), Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(UIActivity1.this, "取消了" + cb.getText(), Toast.LENGTH_LONG).show();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
tools:context="com.hanqi.test5.UIActivity1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择Android的开发语言是什么?"/> <RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rg"> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++"
android:id="@+id/rb1"
android:layout_marginRight="30dp"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/rb2"
android:layout_marginRight="30dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JAVA"
android:id="@+id/rb3"
android:layout_marginRight="30dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C#"
android:id="@+id/rb4" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择字体效果"/> <CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="宋体"
android:id="@+id/cb_st"
android:checked="true"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加粗"
android:id="@+id/cb_jc" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="斜体"
android:id="@+id/cb_xt" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下划线"
android:id="@+id/cb_xhx" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/off"
android:id="@+id/iv"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="打开"
android:textOff="关闭"
android:id="@+id/tob"/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开关"
android:textOff="关"
android:textOn="开"
android:id="@+id/sw"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:background="@drawable/mybutton"
android:id="@+id/tob1"/> </LinearLayout>

特别注意,怎么能让按钮不显示字体,用两张背景图来显示效果呢?这就要用到一段xml代码了,********  这一段代码的名字一定要全部小写

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/on" android:state_checked="true"/>
<item android:drawable="@drawable/off" android:state_checked="false"/>
</selector>

实现效果:

Android课程---视图组件之开关按钮的更多相关文章

  1. Android课程---视图组件总结(2)

  2. Android课程---视图组件总结

  3. Android课程---视图组件的总结

  4. Android课程---优化ListView列表视图(2)

    layout_simple.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  5. Android——滚动视图(ScrollView)图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟

    xml <?xml version="1.0" encoding="utf-8"?> <!--滚动视图--> <ScrollVie ...

  6. Android的视图(View)组件

    Android的绝大部分UI组件都放在android.widget包及其子包.android,view包及其子包中,Android应用的所有UI组件都继承了View类,View组件非常类似于Swing ...

  7. 一培训机构设计的学习android课程内容:供大家参考

    转自:http://www.cnblogs.com/csj007523/archive/2011/06/16/2082682.html 一培训机构设计的学习android课程内容:供大家参考 第一阶段 ...

  8. Android开发之 Android 的基本组件的概述

    Android是一个为组件化而搭建的平台,它的应用是由一些零散的有联系的组件组成,并通过AndroidManifest.xml文件 把它们绑定起来. Android常用的组件有: Activity(活 ...

  9. Android编程: fragment组件、菜单和Intent组件

    学习内容:fragment组件.菜单和Intent组件 ====fragment组件====1.fragment是一种自我容纳,模块化的,嵌入在一个Activity里面的视图组件    可以在运行时动 ...

随机推荐

  1. 《DSP using MATLAB》示例Example4.12

    上代码: b = [0, 1, 1]; a = [1, -0.9, 0.81]; % [R, p, C] = residuez(b,a); Mp = (abs(p))' Ap = (angle(p)) ...

  2. 《DSP using MATLAB》 示例Example4.1

    今天开始看第4章,从开始看这本书到现在,过去一个多月,收获不少,继续坚持.

  3. 20145223《Java程序程序设计》实验报告二

    实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步骤 (一)单元测试 ...

  4. Android自动化测试 - MonkeyRunner(二) 锤子便签测试脚本

    来源于:http://testerhome.com/topics/878 # encoding=utf-8 #导入python中自带的time模块和sys模块,脚本中都要用到它们. import ti ...

  5. Bridge模式——对象结构型模式

    今天看了Bridge模式,对其进行简单的总结,并给出几篇通俗易懂的文章链接. (一)意图--将抽象部分和它的实现部分分离,使它们都可以独立地变化. 适用于从多维度描述的类型,拆解开来,使其能沿着各维度 ...

  6. The 2015 China Collegiate Programming Contest L. Huatuo's Medicine hdu 5551

    Huatuo's Medicine Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  7. ccc 正态分布

    cc.Class({ extends: cc.Component, properties: { prefab: { default:null, type:cc.Prefab }, root: { de ...

  8. BZOJ4107 : [Wf2015]Asteroids

    首先将速度相减,变成A在动而B不动,若速度为0则显然永远不会相交. 枚举A的每个点以及B的每条线段,计算这三个点共线的时刻. 将时刻排序,对于每个区间进行三分,用半平面交计算相交面积. 注意特判相交面 ...

  9. POJ1419 & 最大团

    题意: 求一个图的最大点独立集.SOL: 转化为补图的最大团,最大团似乎是一个NP问题,那么只好爆搜了. 补一补图论基础,代码不想打了,来自某blog #include <iostream> ...

  10. 洛谷 P1462 通往奥格瑞玛的道路 Label: 最小化最大值 && spfa (存多条边示例)

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...