onClick事件不会触发|安卓 [英] onClick event is not triggering | Android

查看:217
本文介绍了onClick事件不会触发|安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个非常简单的测试应用程序有一个活动,一个布局。该的onClick 不触发第一次是pressed,因为它应该。

I made a very simple test application with one activity and one layout. The onClick doesn't trigger the first time it is pressed, as it should.

活动:

package com.example.mytest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText ed1 = (EditText) findViewById(R.id.editText1);

        ed1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_LONG)
                        .show();

            }

        });
    }

}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:ems="10" />

</RelativeLayout>

如果你运行这个应用程序,并单击第二个 EDITTEXT ,然后再打开第一个,也不会触发的onClick 。您可以选择保持来回,它不会在所有触发的onClick 。我需要这个基本功能,但一直没能想办法来得到它的工作。想法?

If you run this application, and click on the second editText and then back on the first one, it will not trigger the onClick. You can keep selecting back and forth and it will not trigger the onClick at all. I need this basic functionality, but haven't been able to think of a way to get it to work. Ideas?

我已经尝试了所有的建议对我的API级别16物理设备和我的API级别8模拟器的方法,但我得到了同样的问题。

I have tried all of the methods recommended on my API level 16 physical device and my API level 8 emulator, but I get the same problem.

editText1 聚焦并点击,那么的onClick 方法火灾。如果 editText2 被聚焦,然后 editText1 被点击时,它的火灾。大问题。

When editText1 is focused and is clicked on, then the onClick method fires. If editText2 is focussed, and then editText1 is clicked, it doesn't fire. Big problem.

推荐答案

当用户与不同的听众被称为一个自上而下的顺序UI元素进行交互。 (例如:OnTouch - > OnFocusChange - >的OnClick。)如果一个监听器被定义(与西顿...监听器),它会消耗该事件:低优先级的监听器不会被调用。从本质上你第一次接触它接收到OnFocusChangeListener重点,使用户可以键入一个EditText。该行动在这里为此消耗的OnClick不叫。每个连续的触摸不会改变焦点,这样的事件淌下下降到OnClickListener。

When a user interacts with a UI element the various listeners are called in a top down order. (For example: OnTouch -> OnFocusChange -> OnClick.) If a listener has been defined (with setOn...Listener) and it consumes this event: the lower priority listeners will not be called. By its nature the first time you touch an EditText it receives focus with OnFocusChangeListener so that the user can type. The action is consumed here therefor OnClick is not called. Each successive touch doesn't change the focus so the event trickles down to the OnClickListener.

基本上,你有三种选择:

  1. 设置可聚焦属性设置为false在你的XML:

  1. Set the focusable attribute to false in your XML:

android:focusable="false"

现在的OnClickListener会火的每次的时间被点击。但是,这使得EditText上无用的,因为用户无法再输入任何文字...

Now the OnClickListener will fire every time it is clicked. But this makes the EditText useless since the user can no longer enter any text...

随着OnClickListener实现一个OnFocusChangeListener:

Implement an OnFocusChangeListener along with the OnClickListener:

ed1.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus)
            Toast.makeText(getApplicationContext(), "onFocusChange", Toast.LENGTH_LONG).show();
    }
});

总之,你可以赶上你的EditText每一个触摸事件。

Together you can catch every touch event on your EditText.

本身实现一个OnTouchListener:

Implement an OnTouchListener by itself:

ed1.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(MotionEvent.ACTION_UP == event.getAction())
            Toast.makeText(getApplicationContext(), "onTouch: Up", Toast.LENGTH_SHORT).show();
        return false;
    }
});

这将执行每次的EditText被触摸的时间。请注意,这个事件返回一个布尔值。返回表示,此次活动将持续涓滴和到达建在onFocusChangeListener允许它接收文本。

This will execute every time the EditText is touched. Notice that this event returns a boolean. Returning false means that the event will continue trickle down and reach the built in onFocusChangeListener allowing it to receive text.

希望帮助!

这篇关于onClick事件不会触发|安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆