切换按钮android

ros*_*oss 3 android toggle

我希望我的切换按钮的ON文本很大而OFF文本很小.但不能这样做..有什么建议吗?这就是我的尝试

mf=(ToggleButton)findViewById(R.id.mf);

        if(mf.isEnabled()==true)
        {
            mf.setTextSize(13);
        }
        else
            mf.setTextSize(8);
Run Code Online (Sandbox Code Playgroud)

Zol*_*eas 15

每次单击按钮时都必须调用代码.所以使用:

toggleButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (toggleButton.isChecked()) {
            toggleButton.setTextSize(13.0f);
        } else {
            toggleButton.setTextSize(8.0f);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

您可以使用简单方法设置OnClickListner.在.xml中放置选项

android:onClick="nameOfMethod" 
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中:

public void nameOfMethod(View v){

}
Run Code Online (Sandbox Code Playgroud)

其中v是这种情况下的togglebutton(ToggleButton mf =(ToggleButton)v;)