【问题标题】:android-how can a button remain in "clicked appearance" even after realease?android-即使在发布后,按钮如何保持“点击外观”?
【发布时间】:2013-09-07 14:53:15
【问题描述】:

在 Holo 主题中,当您单击一个按钮时,它会变为蓝色并闪烁片刻。现在我希望按钮保持这种外观,并在下次单击时恢复正常外观。怎么做?

更新:我的代码:

public class HomeActivity extends SherlockActivity {
org.holoeverywhere.widget.Button bt;
boolean isPressed = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_layout);
    bt = (org.holoeverywhere.widget.Button) findViewById(android.R.id.button1);
    bt.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (!isPressed) {
                    isPressed = true;
                } else {
                    isPressed = false;
                }
                bt.setPressed(isPressed);
            }

            return true;
        }
    });
}
}

【问题讨论】:

  • @AshaSoman using any images ?!我想要的根本与图像无关
  • 您可以使用自定义图像或颜色为此按钮设置选择器。
  • @AshaSoman 请解释更多,代码示例,谢谢

标签: java android button click


【解决方案1】:

方法一:可以为这个按钮设置一个可绘制的选择器

1.Create a file `buttonbg.xml` in `drawable/`

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

        <item
            android:state_focused="true"
            android:drawable="@drawable/focused_button" />
        <item
            android:state_pressed="true"
            android:drawable="@drawable/pressed_button" />
        <item
            android:drawable="@drawable/normal_button" />
    </selector>

2. Create necessary images for indicating states of button

3. Set this buttonbg.xml as background for this button.

4. set this button setPressed(true) inside button click. 

欲了解更多信息:How to modify the default button state in Android without affecting the pressed and selected states?

方法二:

boolean isPressed = false;
((Button)findViewById(R.id.button)).setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP){
                if(!isPressed){
                    isPressed = true;
                }else{
                    isPressed = false;
                }  
                        ((Button)findViewById(R.id.button)).setPressed(isPressed);                      
            }

                return true;
            }
        });

【讨论】:

  • 不幸的是,两者都不起作用,@drawable/focused_button@drawable/pressed_button@drawable/normal_button 找不到...第二种方法导致强制关闭:(
  • 请您的平面设计师为按下的按钮、普通按钮等创建图像。并使用它们的名称而不是答案中给出的名称。这只是一个指导您实现目标的示例。什么是第二种方法得到的异常吗?
  • 我想通过onCreate() 中的setOnTouchListener 方法将TouchListener 附加到我的按钮上,当我这样做时,我的应用程序被强制关闭...为什么?
  • 你添加了 ActionBarSherlock 库吗?
  • +1 是的,实际上您的第一种方法的理论是正确的,最终解决了我的问题,但是正如@Nizam 所述,我不需要自定义图像...
【解决方案2】:

首先,您可以从该位置获取 SDK 使用的默认图像(按钮、复选框,无论是什么) ->

~\android-sdk\platforms\android-18\data\res\drawable-hdpi

我在这里寻找 API-18。使用任何你喜欢的支持 Holo 主题的版本。

从那里,您可以找到类似 btn_default_pressed_holo_dark.9.png 的图像。

将其复制并粘贴到您的项目 drawable-hdpi 文件夹中。这是第一步。现在,

public class MainActivity extends Activity {
    Button b;
    Drawable back;
    int flag=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b=(Button) findViewById(R.id.button1);

        back=b.getBackground();

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {   
                if(flag==0){
                    b.setBackgroundResource(R.drawable.btn_default_pressed_holo_dark);
                    flag=1;
                    }
                else{
                    flag=0;
                    b.setBackground(back);
                }
            }
        });
    }


}

检查一下:)

【讨论】:

  • +1 太棒了!工作得很好:) 但是我必须将 b.setBackground(back); 更改为 b.setBackgroundDrawable(back); 以便它工作(但它已被弃用!)...谢谢
  • 不客气 :) 是的,.setBackgroundDrawable() 从 API 级别 16 开始已被弃用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 1970-01-01
  • 1970-01-01
  • 2017-12-25
  • 2011-03-26
  • 2019-10-04
  • 1970-01-01
相关资源
最近更新 更多