0

我有图像按钮,我想用切换单击(如切换按钮)编写 setOnClickListener 方法。我知道切换按钮的工作原理,但我不需要使用它。可以在 imagebutton 中编写 toggleclick 方法。我写了一些代码但没有工作,例如切换按钮

strada_chart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(arg0.isClickable()==true)
            {
                Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
            }
        }
    });
4

3 回答 3

1

尝试这个..

likeBtn.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        if(fun) {
            likeBtn.setImageResource(R.drawable.unlike);
            fun=false;
        } else {
            fun=true;       
            likeBtn.setImageResource(R.drawable.like);
            Toast.makeText(
                    getApplicationContext(),
                    "Changed",
                    Toast.LENGTH_LONG
            ).show();
        }
    }
});
于 2014-06-27T11:21:48.250 回答
0

尝试这个....

private static boolean isClicked = true;
strada_chart.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        if(isClicked)
        {
            Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
            isClicked = false;
        }
        else
        {
            Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
        }

    }
});
于 2014-06-27T11:40:41.940 回答
0
public void onToggleClicked(View view) {
        // Is the toggle on?
        switch (view.getId()) {
        case R.id.xyz:
            boolean on = ((ToggleButton) view).isChecked();

            if (on) {


                Toast.makeText(mContext, "A", Toast.LENGTH_SHORT).show();
            } else {


                Toast.makeText(mContext, "B", Toast.LENGTH_SHORT).show();
            }
        }
    }

使用切换按钮的属性

在 XML 中:

<ToggleButton
            android:id="@+id/xyz"
            style="@style/toggleButton"
            android:layout_width="190dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_margin="8dp"
            android:background="@drawable/ic_toggle_sv"
            android:onClick="onToggleClicked" />

在 ic_toggle_sv

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+android:id/background"
        android:drawable="@android:color/transparent"/>
    <item
        android:id="@+android:id/toggle"
        android:drawable="@drawable/ic_toggle1"/>

</layer-list>

在 ic_toggle1 中传递切换按钮的两个图像

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_toggle_stills" android:state_checked="false"/>
    <item android:drawable="@drawable/ic_toggle_videos" android:state_checked="true"/>

</selector>
于 2014-06-27T11:31:16.097 回答