我有图像按钮,我想用切换点击编写setOnClickListener方法(如toggleButton)。我知道如何工作togglebutton但我不需要使用它。可以在imagebutton中编写toggleclick方法。我写了一些代码,但没有工作,如togglebutton
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();
}
}
});
答案 0 :(得分:1)
试试这个..
Like_btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if(fun)
{
Like_btn.setImageResource(R.drawable.unlike);
fun=false;
}
else
{
fun=true;
Like_btn.setImageResource(R.drawable.like);
Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
}
}
});
答案 1 :(得分: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>
答案 2 :(得分: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();
}
}
});