ToggleButton为开关按钮,也就是说点一下就显示开启再点一下就显示关闭。
1.setTextOn当按钮按下时的文字
2.setTextOff当按钮未选取时的文字
3.setChecked设定按纽按下後的状态
4.setBackgroundDrawable设定按纽背景图案
5.setOnCheckedChangeListener设定按纽状态监听器
例如按下ToggleButton後TextView显示ToggleButton目前关闭还是开启
xml档如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:textOff="关闭"
android:textOn="开启"
android:textSize="30dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""
android:textSize="30dp" />
</LinearLayout>
透过setOnCheckedChangeListener来抓取目前状态
MainActivity.java范例:
package com.example.ittogglebtn;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
ToggleButton toggleButton;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = findViewById(R.id.toggleButton);
textView = findViewById(R.id.textView);
toggleButton.setOnCheckedChangeListener(new Toggle());
}
private class Toggle implements CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
textView.setText("ToggleButton开启");
}
else {
textView.setText("ToggleButton关闭");
}
}
}
}
APP画面:
>>: 追求JS小姊姊系列 Day18 -- 方函式的能力展现:第三型态 Constructor function 登场
标题很丑,只希望当有遇到相同问题时,能找到这篇解决问题~~ 大家好,我是Sean,既上偏Heroku...
其实原本最初规画想要做Index方式的纪录,然後多增加一些没写到的面向 不过,总是计画赶不上变化 ...
霓虹灯文字 教学原文参考:霓虹灯文字 这篇文章会介绍在 GIMP 里使用选取、选取边框、填色、文字、...
前情提要 上回提到了要透过 AJAX 远距离魔法呼叫精灵并等待精灵回覆。 艾草:「来,我们先来试试看...
Formatting Context 所有的HTML元素,在CSS里都可以视为box(盒子),在No...