3

单击按钮时,应在按钮的最左角显示一个复选标记图标,当再次单击同一按钮时,复选标记图标应消失。在这种情况下有人可以帮助我吗?

4

3 回答 3

7

虽然已经回答了这个问题,但这里有一个替代解决方案:添加一个 unicode 复选标记符号。其中有两个:\u2713 和 \u2714。只需将它们添加到您的字符串中:

<string name="button_label_on">\u2713 on</string>
<string name="button_label_off">off</string>

当然,您也可以将其直接放入布局代码中:

<Button
   ...
   android:text="\u2713 on"
   />
于 2012-04-10T01:23:12.517 回答
3

您可以在 Button 的左侧添加一个可见性 Gone 的 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);
                }
            }
        });
于 2012-03-12T06:50:05.357 回答
0

签出CheckBox小部件。

于 2012-03-12T05:15:29.583 回答