点击按钮后,应在按钮的左上角显示一个勾选标记图标。再次点击同一按钮时,该勾选标记图标应消失。请问有人能帮我解决这个问题吗?
虽然这个问题已经有答案了,但是这里提供一种替代解决方案:添加一个Unicode的勾选符号。有两个可用:\u2713和\u2714。只需要将它们添加到你的字符串中:
<string name="button_label_on">\u2713 on</string>
<string name="button_label_off">off</string>
<Button
...
android:text="\u2713 on"
/>
您可以在按钮左侧添加一个ImageView(比如tick.png),并将其可见性设置为隐藏。然后设置其可见性。以下是代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/iv_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="@drawable/tick"/>
<Button
android:id="@+id/btn_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press"/>
</LinearLayout>
现在,在按钮单击事件中,您可以设置其可见性:
Button btn_tick = (Button)findViewById(R.id.btn_tick);
btn_tick.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
ImageView iv_tick = (ImageView)findViewById(R.id.iv_tick);
int visibility = iv_tick.getVisibility();
if(visibility == View.VISIBLE)
{
iv_tick.setVisibility(View.GONE);
}
else
{
iv_tick.setVisibility(View.VISIBLE);
}
}
});
请查看复选框小部件。