<togglebutton> onClickListener - 将drawable改回默认值</togglebutton>

时间:2012-01-30 09:59:57

标签: java android onclick togglebutton

我有这个ToggleButton应该在打开/关闭时更改其他按钮的drawables(和函数)。

到目前为止,我只是通过使用此代码设法让它以单向工作。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    toggle = (ToggleButton) findViewById(R.id.bRedGreen);
    toggle.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            minuskugle = (Button) findViewById(R.id.bBallhole);
            minuskugle.setBackgroundResource(R.drawable.redballinhole);

        }

    });
}

如何在另一次点击后将其更改为默认值?

_ __ _ __ _ __ _ __ _ 的__ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ < / EM> __ _ __ _ __ _ __ _ __ _ __ < EM> _

这适用于一个条目。

但是,如果我添加多个条目,则会出现“else”错误。

这有效:

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            pluskugle = (Button) findViewById(R.id.bBallhole);
            minuskugle = (Button) findViewById(R.id.bBallhole);

            pluskegle = (Button) findViewById(R.id.bKegle);
            minuskegle = (Button) findViewById(R.id.bKegle);

        if(toggle.isChecked())
            minuskugle.setBackgroundResource(R.drawable.redballinhole);
        else
            pluskugle.setBackgroundResource(R.drawable.whiteballhole);

这不是

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            pluskugle = (Button) findViewById(R.id.bBallhole);
            minuskugle = (Button) findViewById(R.id.bBallhole);

            pluskegle = (Button) findViewById(R.id.bKegle);
            minuskegle = (Button) findViewById(R.id.bKegle);

        if(toggle.isChecked())
            minuskugle.setBackgroundResource(R.drawable.redballinhole);
            minuskegle.setBackgroundResource(R.drawable.redkegle);
        else
            pluskugle.setBackgroundResource(R.drawable.whiteballhole);
            pluskegle.setBackgroundResource(R.drawable.redkegle);

2 个答案:

答案 0 :(得分:1)

试试这个:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    toggle = (ToggleButton) findViewById(R.id.bRedGreen);
    toggle.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            minuskugle = (Button) findViewById(R.id.bBallhole);
            if(toggle.isChecked())
               minuskugle.setBackgroundResource(R.drawable.redballinhole);
            else
               minuskugle.setBackgroundResource(R.drawable.xy);


        }

    });
}

答案 1 :(得分:1)

虚拟解决方案

保留一个布尔值以保存状态

boolean isPressed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    toggle = (ToggleButton) findViewById(R.id.bRedGreen);
    toggle.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            minuskugle = (Button) findViewById(R.id.bBallhole);
            if(!isPressed)
                minuskugle.setBackgroundResource(R.drawable.redballinhole);
            else
                minuskugle.setBackgroundResource(R.drawable.yourotherdrawable);
            isPressed = !isPressed;
        }
    });
}