ToggleButton的学习与使用

ToggleButton作用:用于控制某个View的状态,参数,内容等,使得对应的值循环转换。


使用第一步:
1,在XML之中定于它,并且写上它想控制的视图。
<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="wrap_content"
android:orientation="vertical" >


<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOff="横向排列横向排列"
android:textOn="纵向排列横向排列" />


<LinearLayout
android:id="@+id/test"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button
android:id="@+id/btone"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮一" />

<Button
android:id="@+id/bttwo"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮二" />

<Button
android:id="@+id/btthree"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮三" />
</LinearLayout>

</LinearLayout>



第二部:在Activity里设置ToggleButton的监听器。

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

public class ToggleButtonActivity extends Activity {
private Button button = null;
private EditText editText = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.togglebutton);
// Listen for button clicks
ToggleButton toggle=(ToggleButton)findViewById(R.id.toggle);
final LinearLayout test=(LinearLayout)findViewById(R.id.test);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){


@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(arg1){
//设置LinearLayout垂直布局
test.setOrientation(LinearLayout.VERTICAL);
}
else{
//设置LinearLayout水平布局
test.setOrientation(LinearLayout.HORIZONTAL);
}
}
});
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值