单击事件在框架布局内不起作用

时间:2017-11-10 07:22:35

标签: android click

public class GameView extends AppCompatActivity {

    @InjectView(R.id.back_btn_game_view)
    Button backBtnGameView;

    public static Context context;
    public static TextView matchNameView;
    public static String currentState = "";
    private AssetsPropertyReader assetsPropertyReader;
    private Properties skorkastProperties;
    private String skorkastMgntURL;
    public static Socket mSocket;
    private String skorkastSocketURL;
    public static boolean userConnected;
    public HashMap<String, Variables> variableHashMap = new HashMap<>();
    public static TextView timer;


    public static long millisInFuture = 0;
    public static long countDownInterval = 1000;
    public static CountDownTimer countDownTimer;
    public static long timeRemaining = 0;
    public static boolean isPaused = false;
    public static boolean isCanceled = false;
    public static String timeLeft = "", gameStatus = "";
    public static int periodCount = 0;
    public static int[] periodArray = new int[3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_view);
        butterknife.inject(this);
        matchNameView = (TextView) findViewById(R.id.match_name);
        timer = (TextView) findViewById(R.id.timer);

        context = GameView.this;
        assetsPropertyReader = new AssetsPropertyReader(context);
        skorkastProperties = assetsPropertyReader.getProperties("skorkast.properties");
        skorkastMgntURL = skorkastProperties.getProperty("SKORKAST_MGNT_URL");
        skorkastSocketURL = skorkastProperties.getProperty("SKORKAST_SOCKET_URL");
        userConnected = false;

        String matchname = getIntent().getStringExtra("matchname");
        matchNameView.setText(matchname);

        // Check that the activity is using the layout version with
        // the fragment_container FrameLayout
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }
        }

        try {
            mSocket = IO.socket(skorkastSocketURL);
            mSocket.connect();

            JSONObject joinObj = new JSONObject();
            try {
                joinObj.put("name", "scorer");
                joinObj.put("match", matchname);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (!GameView.userConnected) {
                GameView.mSocket.emit("join", joinObj, new Ack() {
                    @Override
                    public void call(Object... args) {
                        GameView.userConnected = true;
                        Log.i("TAG", "connected");
                    }
                });
            } else {
                Log.i("TAG", "already connected");
            }
        } catch (URISyntaxException e) {
            e.printStackTrace();
            Log.e("TAG", "error connect socket " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("TAG", "error connect socket " + e.getMessage());
        }

        Data.changeMode(2);
        Data.changeState(3, 4);


        //when the user click back button, it need to set the initiate normal play as state--> need to remove

        if (Data.currentState.getName().equals("initiate_normal_play")) {
            if (Data.currentState.getCurrentView().getOrientation().equalsIgnoreCase("vertical")) {
                VerticalFragment vFragment = new VerticalFragment();
                vFragment.setArguments(getIntent().getExtras());
                getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, vFragment).commit();
            } else {
                HorizontalFragment hFragment = new HorizontalFragment();
                hFragment.setArguments(getIntent().getExtras());
                getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, hFragment).commit();
            }
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        SystemUiHelper uiHelper = new SystemUiHelper(this, SystemUiHelper.LEVEL_IMMERSIVE, flags);
        uiHelper.hide();
    }


    @Override
    public void onBackPressed() {
        Log.i("TAG", "Fragment stack count :: " + getSupportFragmentManager().getBackStackEntryCount());
        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {

            if (GameView.mSocket != null && GameView.mSocket.connected()) {
                GameView.mSocket.emit("userdisconnect");
            }
            this.finish();
        } else {
            getSupportFragmentManager().popBackStack();
        }


    }
@OnClick(R.id.back_btn_game_view)
            public void onViewClicked () {
                Toast.makeText(this, "working finally",Toast.LENGTH_SHORT).show();
        }
}

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:background="@drawable/icehockey_bg"
    android:orientation="horizontal">

<FrameLayout
    android:clickable="true"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <RelativeLayout
        android:clickable="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/back_btn_game_view"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:clickable="true"
            android:cropToPadding="true"
            android:scaleType="fitXY"
            android:src="@drawable/ic_back_button" />

        <TextView
            android:id="@+id/match_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="Match Name"
            android:textAllCaps="true"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/timer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="02:00"
            android:textColor="#000000"
            android:textSize="20sp"
            android:textStyle="bold" />
    </RelativeLayout>
</FrameLayout>

</LinearLayout>

我在Fragment布局中有Activity。我在click布局中创建了Button的{​​{1}}事件。当我从Activity点击后面Button时,根本没有回复。   但是我尝试将Fragment移到Button frame layout之内,它正常工作,我不明白为什么它在框架布局中无法正常工作。请帮我解决这个问题。

LinearLayout:我通过给布局充气来尝试点击在片段内部工作。

4 个答案:

答案 0 :(得分:2)

这是因为您已将android:clickable="true"设置为父RelativeLayout。所以RelativeLayout正在窃取其点击事件。尝试删除它,使其工作。

答案 1 :(得分:0)

试试这个。 我把你的按钮放在frameLayout中的textView之后。 希望它能奏效。

RelativeLayout

`

答案 2 :(得分:0)

LinearLayout FrameLayoutandroid:clickable="true" 中删除此内容。然后尝试。

findViewById(R.id.back_btn_game_view).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(YouActivity.this, "back_btn_game_view", Toast.LENGTH_SHORT).show();
        }
    });
findViewById(R.id.fragment_container).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(YouActivity.this, "fragment_container", Toast.LENGTH_SHORT).show();
        }
});

修改

我测试你的代码。它工作正常。你可以在你的代码中尝试这个。

onViewClicked

修改

您的onBackPressed采用@OnClick(R.id.back_btn_game_view) public void onViewClicked() { Toast.makeText(this, "working finally", Toast.LENGTH_SHORT).show(); } 方法。因此将其移到外面。

<div class="w3-top">
    <!-- Navigation bar section-->
    <div class="w3-bar w3-card w3-animate-top w3-green" id="myNavbar">
        <a href="#" class="w3-bar-item "> <img src="img/Grawia.bmp" height="50"></a>
        <a href="#" class="w3-bar-item w3-button">Home</a>
        <a href="#" class="w3-bar-item w3-button">Products</a>
        <a href="#" class="w3-bar-item w3-button">About Us</a>
        <a href="#" class="w3-bar-item w3-button">Contact Us</a>
        <a href="#" class="w3-bar-item w3-button w3-right"><i class="fa fa-search"></i></a>
    </div>
    <!-- END OF Navigation bar section -->
</div>
<!-- Swiper -->
<div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide">Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
    </div>
    <!-- Add Pagination -->
    <div class="swiper-pagination"></div>
    <!-- Add Arrows -->
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
</div>

<!-- Swiper JS -->
<script src="js/swiper.min.js"></script>

<!-- Initialize Swiper -->
<script>
    // Change style of navbar on scroll

    window.onscroll = function() {
        scrollEffect()
    };

    function scrollEffect() {
        var navbar = document.getElementById("myNavbar");
        if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
            navbar.className = "w3-bar" + " w3-card" + " w3-animate-top" + " w3-red";
        } else {
            navbar.className = navbar.className.replace("w3-bar w3-card w3-animate-top w3-red", "w3-bar w3-card w3-animate-top w3-red");
        }

    }

    var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',
        paginationClickable: true,
        spaceBetween: 30,
        centeredSlides: true,
        autoplay: 2500,
        autoplayDisableOnInteraction: false
    });
</script>

答案 3 :(得分:0)

尝试从RelativeLayout删除此行。

...
<FrameLayout
    android:clickable="true"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <RelativeLayout
        android:clickable="true" // Remove this line
...