Android的按钮 - 如何设置可聚焦为true,仍然接受第一次点击的onClick监听器?监听器、如何设置、按钮、Android

2023-09-06 09:16:06 作者:与其埋怨世界,不如改变自己。本期小编为正在努力拼搏的小伙伴们

更新

我从按钮样式卸下两可聚焦线和使用onClick事件处理函数调用解决了这个问题,点击 requestFocusFromTouch();

I've solved the clicking issue by removing the two focusable lines from the button style and using the onClick event handler to call requestFocusFromTouch();

不幸的是我留下了一个问题,requestFocusFromTouch()的重点错了按钮。它总是聚焦的第一个按钮而不是按钮,我呼吁方法。这是一款Android错误或预期的行为,我,我可以做一个变通?

Unfortunately I'm left with an issue that requestFocusFromTouch() focuses the wrong button. It always focuses the first button not the button I call the method on. Is this an android bug or expected behaviour and what I can I do for a work around?

在code现在看起来是这样的onClick事件

The code now looks like this for the onClick event

public void onClick(View v) {
    switch (v.getId()) {
            case R.id.btn_home:
                Intent i = new Intent(this, QuizMenu.class);
                startActivity(i);
                v.requestFocusFromTouch();
            break;
            case R.id.btn_support:
                Intent i1 = new Intent(this, Support.class);
                startActivity(i1);
                v.requestFocusFromTouch();
                View btnSupport = findViewById(R.id.btn_support);
                btnSupport.setSelected(true);
                btnSupport.requestFocusFromTouch();
            break;
            // More buttons go here (if any) ...
    }
}

END OF UPDATE

我有一个机器人按钮具有以下样式

I have an android button that has the following style

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MnuBtnStyle">
        <item name="android:ellipsize">marquee</item>
        <item name="android:scrollHorizontally">true</item>
        <item name="android:focusable">true</item>
        <item name="android:focusableInTouchMode">true</item>
        <item name="android:singleLine">true</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_marginTop">1dip</item> 
        <item name="android:layout_marginBottom">1dip</item>
        <item name="android:textSize">10dp</item>
        <item name="android:layout_weight">1</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>

我有一个上点击监听器,看起来像这样

I have an on click listener that looks like this

public void onClick(View v) {
    switch (v.getId()) {
            case R.id.btn_home:
                Intent i = new Intent(this, HomeScreen.class);
                startActivity(i);
            break;
            // More buttons go here ...
    }
}

予有上的按钮图像,并且选择器看起来像这样

I have an image on the button and the selector looks like this

<?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/ic_tab_home_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
        android:drawable="@drawable/ic_tab_home_pressed" /> <!-- pressed -->
    <item android:drawable="@drawable/ic_tab_home_unpressed" /> <!-- default/unchecked -->
</selector>

使用该按钮,该按钮看起来像这样

The layout that uses the button and the button looks like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
      android:id="@+id/top_menu_bar"
      android:layout_marginTop="2dip" 
      android:padding="0dip"
      android:background="@android:drawable/bottom_bar"
      android:layout_alignParentTop="true"
      android:gravity="top"
      android:layout_height="@dimen/menu_bar_height"
      android:layout_width="fill_parent">
      <Button
          android:id="@+id/btn_home"
          android:text="@string/btn_home"
          android:drawableTop="@drawable/home_button"
          style="@style/MnuBtnStyle"
      />
...

有其他按键和该布局充当包含在任何布局需要它的顶菜单栏。

there are other buttons and this layout is acting as a top menu bar that is included in whatever layout needs it.

onclick事件的按钮只发射按钮的第二次点击,由于可获得焦点行。

The onClick event for the button is only fired on second click of the button due to the focusable lines.

        <item name="android:focusable">true</item>
        <item name="android:focusableInTouchMode">true</item>

第一次点击将焦点设置按钮,然后第二次点击触发onClick事件监听器 我需要将两者结合在一起,使第一次点击将焦点设置并触发onClick事件侦听器。可以这样做?

The first click sets focus on the button then the second click fires the onClick event listener I need to combine the two together so that the first click sets focus AND fires the onClick event listener. Can this be done?

如果我卸下两个可聚焦线按钮的行为正常,但我想表明按钮的重点是,我想要的按钮内部的滚动文字&LT;项目名称=机器人:ellipsize &GT;字幕&LT; /项目&GT; 给我

If I remove the two focusable lines the buttons behave normally but I want to show that the button is focused and I want the scrolling text inside the button that <item name="android:ellipsize">marquee</item> gives me

pciated任何想法大大AP $ P $

Any ideas greatly appreciated

推荐答案

您可能需要使用一个 OnFocusChangeListener ,然后检查是否当屏幕处于触摸模式的焦点事件发生。如果它没有那么它是一个用户点击,否则它是由轨迹球/触控板

You might need to use an OnFocusChangeListener and then check if the focus event happened when the screen was in touch mode: if it did then it was a user click, otherwise it was from the trackball/trackpad.

 
精彩推荐
图片推荐