开发者

Toggle button android

开发者 https://www.devze.com 2023-04-01 14:37 出处:网络
I want my toggle button\'s ON text to be large and OFF text to be small. But can\'t do it.. any suggestions? This is what i was 开发者_运维百科trying

I want my toggle button's ON text to be large and OFF text to be small. But can't do it.. any suggestions? This is what i was 开发者_运维百科trying

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

        if(mf.isEnabled()==true)
        {
            mf.setTextSize(13);
        }
        else
            mf.setTextSize(8);


Your code has to be called each time you click on your button. so use :

toggleButton.setOnClickListener(new View.OnClickListener() {

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

You can set OnClickListner with a easy method. In your .xml put the option

android:onClick="nameOfMethod" 

And then in your code:

public void nameOfMethod(View v){

}

Where v is the togglebutton in this case ( ToggleButton mf = (ToggleButton)v; )


I put the solution here:

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

    if(mf.isChecked())
    {
        mf.setTextSize(13);
    }
    else
        mf.setTextSize(8);

Use isChecked() instead of isEnabled()


You need to do some debugging.

Firstly:

if(mf.isEnabled()==true) 

can be

if (mf.isEnabled()) 

Does mf.setTextSize(13) on it's own work as expected?

Add some logging:

    if (mf.isEnabled())
    {
        // Add some logging, is this line reached correctly?
        mf.setTextSize(13);
    }
    else
        // Add some logging, is this line reached correctly?
        mf.setTextSize(8);

Chances are you need to change isEnabled() to isChecked(). isEnabled() means exactly that, whether it's enabled or not. You want to know whether the button has been checked.

0

精彩评论

暂无评论...
验证码 换一张
取 消