在本文中,我们将为您详细介绍Android开发之ToggleButton实现开关效果示例的相关知识,并且为您解答关于安卓togglebutton的疑问,此外,我们还会提供一些关于android4.1.
在本文中,我们将为您详细介绍Android开发之ToggleButton实现开关效果示例的相关知识,并且为您解答关于安卓togglebutton的疑问,此外,我们还会提供一些关于android 4.1.2(API16) ToggleButton 中设置 android:background 属性的错误谁遇到过、Android ToggleButton、Android ToggleButton 详解及实例代码、Android ToggleButton:状态切换的Button的有用信息。
本文目录一览:- Android开发之ToggleButton实现开关效果示例(安卓togglebutton)
- android 4.1.2(API16) ToggleButton 中设置 android:background 属性的错误谁遇到过
- Android ToggleButton
- Android ToggleButton 详解及实例代码
- Android ToggleButton:状态切换的Button
Android开发之ToggleButton实现开关效果示例(安卓togglebutton)
本文实例讲述了Android使用ToggleButton实现开关效果的方法。分享给大家供大家参考,具体如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ToggleButton android:checked="false" android:textOn="开" android:textOff="关" android:id="@+id/toggleButton1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/off" /> </LinearLayout>
MainActivity.java
package com.example.hello; import android.support.v7.app.ActionBaractivity; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedchangelistener; import android.widget.ImageView; import android.widget.ToggleButton; import android.os.Bundle; public class MainActivity extends ActionBaractivity implements OnCheckedchangelistener { private ToggleButton tb; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 tb = (ToggleButton) findViewById(R.id.toggleButton1); img = (ImageView) findViewById(R.id.imageView1); //给当前的tb设置监听器 tb.setonCheckedchangelistener(this); } @Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { /* * 当tb被点击的时候,执行当前方法 * buttonView 代表被点击的控件本身 * isChecked 代表被点击的控件的状态 * * 当点击tb的时候,更换img的背景 */ img.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off); } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
android 4.1.2(API16) ToggleButton 中设置 android:background 属性的错误谁遇到过
android 4.1.2(API16) ToggleButton 中设置 android:background 属性的错误谁遇到过?
ToggleButton设置backgroud就会消失,求大神破。
Android ToggleButton
ToggleButton有两种状态:选中和未选择状态,并且需要为不同的状态设置不同的显示文本:
<ToggleButton
android:id="@+id/textview_show_all"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textOn="ON" //选中显示文本
android:textOn="OFF" //未选中显示文本
/>
toggleButton.setOnCheckedChangeListener(); // 对应点击事件---
public CharSequence getTextOff (); // 返回按钮未选中时的文本。返回值 文本
public CharSequence getTextOn (); //返回按钮选中时的文本。 返回值 文本
setBackgroundDrawable (Drawable d); //设置指定的可绘制(译者注:如图片)为背景
setChecked (boolean checked); // 改变按钮的选中状态。
setTextOff (CharSequence textOff); // 设置按钮未选中时显示的文本。
setTextON (CharSequence textOn); // 设置按钮选中时显示的文本。
onFinishInflate ();//XML文件加载视图完成时调用。这个函数在加载的最后阶段被调用,所有的子视图已经被添加。
drawableStateChanged ();//在视图状态的变化影响到所显示可绘制的状态时调用这个方法。
Android ToggleButton 详解及实例代码
Android ToggleButton 详解
在Android的开发过程中,对于ToggleButton的使用频率也是相当的高的,下面我就来说一下,这个组件的两种使用方式。
第一种是简单的使用,利用Toast的方式弹出提示语句
需要注意的是要想自定义ToggleButton的显示的内容,就需要设置其TextOn和TextOff的内容。
<ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/toggleButton2" android:layout_alignBottom="@+id/toggleButton2" android:textOn="开" android:textOff="关" android:layout_alignRight="@+id/imageview" android:text="Simple test" />
然后是主要的显示代码:
case R.id.toggleButton1: if(SimpleTest.isChecked()){ Toast.makeText(getApplication(),"你打开了开按钮",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getApplication(),"你打开了关按钮",Toast.LENGTH_SHORT).show(); } break; //应该注意的是,先声明ToggleButton并初始化,然后注册侦听方法
接下来是一个较为复杂一点的使用案例,那就是配合ImageView来实现不同的图片显示状态
<ToggleButton android:id="@+id/toggleButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/imageview" android:layout_alignParentTop="true" android:layout_marginTop="46dp" android:textOn="美女" android:textOff="图标" android:text="With Image" /> <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/note" android:layout_below="@id/toggleButton2" />
然后是活动代码
case R.id.toggleButton2: if(WithImage.isChecked()){ imageview.setimageResource(R.drawable.note); }else{ imageview.setimageResource(R.drawable.ic_launcher); } break;
需要注意的是,我们同样需要先进行声明,才能对其使用,否则会报空指针的错误。
下面是程序运行之后的结果
总结与设想:
在使用过程中使用到的ToggleButton 一般来说不会这么的简单,但是主要的思想和框架还是基于这里的。我们可以在相关的侦听方法中添加比如静音的处理,或者status的改变等等。这样,我们的应用就会变得更加的灵活了。
Android ToggleButton:状态切换的Button
Android ToggleButton:状态切换的Button
Android ToggleButton和Android Button类似,但是ToggleButton提供了一种选择机制,可以表达Button处于何种状态,比如常见的WiFi打开或者关闭状态等等这种类似与非门的状态机。
标准的Android ToggleButton样式简单,如果要实现自定义的、表达力丰富的ToggleButton,其中一个方式就是写selector.xml文件作为ToggleButton的background,现在写一个例子。
布局文件activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="zhangphil.demo.MainActivity">
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:checked="true"
android:textOff="关闭"
android:textOn="打开"
android:background="@drawable/selector"/>
</RelativeLayout>
activity_main.xml需要的、位于drawable目录下的selector.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/darker_gray" android:state_checked="false" />
<item android:drawable="@android:color/holo_green_light" android:state_checked="true" />
</selector>
上层Java代码:
package zhangphil.demo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton mToggleButton = (ToggleButton) findViewById(R.id.toggleButton);
mToggleButton.setOnCheckedChangeListener(mOnCheckedChangeListener);
}
private ToggleButton.OnCheckedChangeListener mOnCheckedChangeListener = new ToggleButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String s="";
if(isChecked)
s="打开";
else
s="关闭";
Toast.makeText(getApplication(),s,Toast.LENGTH_SHORT).show();
}
};
}
代码运行结果如图:
附录文章:
1,《Android Segmented RadioButton》链接地址:http://blog.csdn.net/zhangphil/article/details/51441677
2,《Android选项切换条SHSegmentControl》链接地址:http://blog.csdn.net/zhangphil/article/details/49720805
关于Android开发之ToggleButton实现开关效果示例和安卓togglebutton的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于android 4.1.2(API16) ToggleButton 中设置 android:background 属性的错误谁遇到过、Android ToggleButton、Android ToggleButton 详解及实例代码、Android ToggleButton:状态切换的Button等相关内容,可以在本站寻找。
本文标签: