更改图像按钮行为

发布于 2024-11-18 11:59:01 字数 857 浏览 7 评论 0原文

拥有此选择器 XML 文件:

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

 <item android:state_pressed="true"
       android:drawable="@drawable/z01_pressed" /> <!-- pressed -->

 <item  android:state_active="true"
        android:drawable="@drawable/z01_pressed" />

 <item android:state_focused="true"
       android:drawable="@drawable/z01_pressed" /> <!-- focused -->

 <item android:drawable="@drawable/z01" /> <!-- default -->

 </selector>

我可以修改它(或以编程方式)以在 Android 中执行此操作:

当您单击并按住 ImageButton 并将手指移动到另一个 ImageButton 时,另一个会获得效果(按下效果)并且第一个恢复到正常状态。

因此,如果您的屏幕上有多个按钮,并且您在它们上面滑动手指,则当手指位于其上方时,每个按钮都会获得按下效果

这可以在 XML 中完成吗?代码?在 API 4 中?或以上?

这可能吗?

谢谢

Having this selector XML file:

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

 <item android:state_pressed="true"
       android:drawable="@drawable/z01_pressed" /> <!-- pressed -->

 <item  android:state_active="true"
        android:drawable="@drawable/z01_pressed" />

 <item android:state_focused="true"
       android:drawable="@drawable/z01_pressed" /> <!-- focused -->

 <item android:drawable="@drawable/z01" /> <!-- default -->

 </selector>

Can I modify it (or have a programmatic way) to do this in Android:

When you click and hold an ImageButton and move you finger to another ImageButton the other one gets the effect (The pressing effect) and the first one returns to its normal state.

So, If you have multiple buttons in your screen and you slide your finger in top of them, each one gets the press effect when the finger is above it

Can this be done in XML? Code? In API 4 ? or above?

Is this even possible?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜清冷一曲。 2024-11-25 11:59:01

对于那些可能感兴趣的人:

我在 API 级别 4 下找不到解决我的问题的方法。所以,我放弃了!

To those who might be interested:

I couldn't find a solution to my problem under API level 4. So, I gave up !

注定孤独终老 2024-11-25 11:59:01

您可以通过为每个按钮使用 onTouchListener 和 Region 对象来完成此操作。首先,您需要找到每个按钮的大小,以便能够确定区域对象的大小:

编辑:

final ImageButton button = (ImageButton) findViewById(R.id.imagebutton);
int width = 128; // The width of the button
int height = 64; // The height of the button
int[] pos = new int[2];
button.getLocationInWindow(pos);

final ImageButton button2 = (ImageButton) findViewById(R.id.imagebutton2);
int width2 = 128; // The width of the button
int height2 = 64; // The height of the button
int[] pos2 = new int[2];
button2.getLocationInWindow(pos2);

final Region region2 = new Region(pos2[0], pos2[1], pos2[0] + width, pos2[1] + height);

button.setOnTouchListener(new OnTouchListener() {
  public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { 
      if(region1.contains((int)event.getX(), (int)event.getY())) {
        button.setImageResource(R.drawable.z01_pressed);
        button2.setImageResource(R.drawable.z01);
      } else if(region2.contains((int)event.getX(), (int)event.getY())) {
        button2.setImageResource(R.drawable.z01_pressed);
        button.setImageResource(R.drawable.z01);
      else {
        button.setImageResource(R.drawable.z01);
        button2.setImageResource(R.drawable.z01);
      }
    }
    else {
      button.setImageResource(R.drawable.z01);
      button2.setImageResource(R.drawable.z01);
    }
    return false;
  }
});

You can do this by using an onTouchListener and a Region object for each button. First you need to find the size of each button to be able to determine the size of the Region objects:

EDIT:

final ImageButton button = (ImageButton) findViewById(R.id.imagebutton);
int width = 128; // The width of the button
int height = 64; // The height of the button
int[] pos = new int[2];
button.getLocationInWindow(pos);

final ImageButton button2 = (ImageButton) findViewById(R.id.imagebutton2);
int width2 = 128; // The width of the button
int height2 = 64; // The height of the button
int[] pos2 = new int[2];
button2.getLocationInWindow(pos2);

final Region region2 = new Region(pos2[0], pos2[1], pos2[0] + width, pos2[1] + height);

button.setOnTouchListener(new OnTouchListener() {
  public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { 
      if(region1.contains((int)event.getX(), (int)event.getY())) {
        button.setImageResource(R.drawable.z01_pressed);
        button2.setImageResource(R.drawable.z01);
      } else if(region2.contains((int)event.getX(), (int)event.getY())) {
        button2.setImageResource(R.drawable.z01_pressed);
        button.setImageResource(R.drawable.z01);
      else {
        button.setImageResource(R.drawable.z01);
        button2.setImageResource(R.drawable.z01);
      }
    }
    else {
      button.setImageResource(R.drawable.z01);
      button2.setImageResource(R.drawable.z01);
    }
    return false;
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文