pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

onClick 事件未觸發(fā) |安卓

onClick event is not triggering | Android(onClick 事件未觸發(fā) |安卓)
本文介紹了onClick 事件未觸發(fā) |安卓的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我制作了一個(gè)非常簡單的測試應(yīng)用程序,其中包含一個(gè)活動(dòng)和一個(gè)布局.onClick 在第一次按下時(shí)不會(huì)觸發(fā),因?yàn)樗鼞?yīng)該觸發(fā).

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.

活動(dòng):

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>

如果你運(yùn)行這個(gè)應(yīng)用程序,然后點(diǎn)擊第二個(gè)editText,然后再點(diǎn)擊第一個(gè),它不會(huì)觸發(fā)onClick.您可以繼續(xù)來回選擇,它根本不會(huì)觸發(fā) onClick.我需要這個(gè)基本功能,但一直想不出一種讓它工作的方法.想法?

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 物理設(shè)備和我的 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.

當(dāng) editText1 獲得焦點(diǎn)并被點(diǎn)擊時(shí),onClick 方法就會(huì)觸發(fā).如果聚焦 editText2,然后單擊 editText1,它不會(huì)觸發(fā).大問題.

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.

推薦答案

概述,當(dāng)用戶與任何 UI 組件交互時(shí),各種偵聽器按自上而下的順序調(diào)用.如果較高優(yōu)先級的偵聽器之一消耗事件",則不會(huì)調(diào)用較低的偵聽器.

Overview, when a user interacts with any UI component the various listeners are called in a top-down order. If one of the higher priority listeners "consumes the event" then the lower listeners will not be called.

在您的情況下,這三個(gè)偵聽器按順序調(diào)用:

In your case these three listeners are called in order:

  1. OnTouchListener
  2. OnFocusChangeListener
  3. OnClickListener

用戶第一次觸摸 EditText 時(shí),它會(huì)獲得焦點(diǎn),以便用戶可以鍵入.操作在這里被消耗.因此較低優(yōu)先級的 OnClickListener 不會(huì)被調(diào)用.每次連續(xù)觸摸都不會(huì)改變焦點(diǎn),因此這些事件會(huì)向下傳遞到 OnClickListener.

The first time the user touches an EditText it receives focus so that the user can type. The action is consumed here. Therefor the lower priority OnClickListener is not called. Each successive touch doesn't change the focus so these events trickle down to the OnClickListener.

按鈕(和其他此類組件)不會(huì)從觸摸事件中獲得焦點(diǎn),這就是每次調(diào)用 OnClickListener 的原因.

Buttons (and other such components) don't receive focus from a touch event, that's why the OnClickListener is called every time.

基本上,您有三個(gè)選擇:

  1. 自己實(shí)現(xiàn)一個(gè) OnTouchListener:

  1. Implement an OnTouchListener by itself:

ed1.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(MotionEvent.ACTION_UP == event.getAction())
            editTextClicked(); // Instead of your Toast
        return false;
    }
});

每次觸摸 EditText 時(shí)都會(huì)執(zhí)行此操作.請注意,偵聽器返回 false,這允許事件向下傳遞到內(nèi)置的 OnFocusChangeListener,它會(huì)更改焦點(diǎn),以便用戶可以鍵入 EditText.

This will execute every time the EditText is touched. Notice that the listener returns false, this allows the event to trickle down to the built-in OnFocusChangeListener which changes the focus so the user can type in the EditText.

與 OnClickListener 一起實(shí)現(xiàn) OnFocusChangeListener:

Implement an OnFocusChangeListener along with the OnClickListener:

ed1.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus)
            editTextClicked(); // Instead of your Toast
    }
});

當(dāng)您的 OnClickListener 捕獲所有其他事件時(shí),此偵聽器會(huì)在焦點(diǎn)更改時(shí)捕獲第一個(gè)觸摸事件.

This listener catches the first touch event when the focus is changed while your OnClickListener catches every other event.

(這不是一個(gè)有效的答案,但知道這是一個(gè)很好的技巧.)在你的 XML 中將 focusable 屬性設(shè)置為 false:

(This isn't a valid answer here, but it is a good trick to know.) Set the focusable attribute to false in your XML:

android:focusable="false"

現(xiàn)在 OnClickListener 將在每次被點(diǎn)擊時(shí)觸發(fā).但這使得 EditText 無用,因?yàn)橛脩魺o法再輸入任何文本...

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...

注意:

getApplicationContext() 會(huì)造成內(nèi)存泄漏.一個(gè)好習(xí)慣是避免使用它,除非絕對必要.您可以安全地改用 v.getContext().

希望有幫助!

這篇關(guān)于onClick 事件未觸發(fā) |安卓的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 裹包机|裹膜机|缠膜机|绕膜机-上海晏陵智能设备有限公司 | 动环监控_机房环境监控_DCIM_机房漏水检测-斯特纽 | 工业废水处理|污水处理厂|废水治理设备工程技术公司-苏州瑞美迪 今日娱乐圈——影视剧集_八卦娱乐_明星八卦_最新娱乐八卦新闻 | 蒸压釜_蒸养釜_蒸压釜厂家-山东鑫泰鑫智能装备有限公司 | 有源电力滤波装置-电力有源滤波器-低压穿排电流互感器|安科瑞 | 挤奶设备过滤纸,牛奶过滤纸,挤奶机过滤袋-济南蓝贝尔工贸有限公司 | 压砖机_电动螺旋压力机_粉末成型压力机_郑州华隆机械tel_0371-60121717 | 口臭的治疗方法,口臭怎么办,怎么除口臭,口臭的原因-口臭治疗网 | 广东风淋室_广东风淋室厂家_广东风淋室价格_广州开源_传递窗_FFU-广州开源净化科技有限公司 | 仓储货架_南京货架_钢制托盘_仓储笼_隔离网_环球零件盒_诺力液压车_货架-南京一品仓储设备制造公司 | 民用音响-拉杆音响-家用音响-ktv专用音响-万昌科技 | 钢结构厂房造价_钢结构厂房预算_轻钢结构厂房_山东三维钢结构公司 | 电位器_轻触开关_USB连接器_广东精密龙电子科技有限公司 | 筛分机|振动筛分机|气流筛分机|筛分机厂家-新乡市大汉振动机械有限公司 | 风化石头制砂机_方解石制砂机_瓷砖石子制砂机_华盛铭厂家 | 灰板纸、灰底白、硬纸板等纸品生产商-金泊纸业 | 智能监控-安防监控-监控系统安装-弱电工程公司_成都万全电子 | 盐水蒸发器,水洗盐设备,冷凝结晶切片机,转鼓切片机,絮凝剂加药系统-无锡瑞司恩机械有限公司 | 低温柔性试验仪-土工布淤堵-沥青车辙试验仪-莱博特(天津)试验机有限公司 | 金属检测机_金属分离器_检针验针机_食品药品金属检探测仪器-广东善安科技 | 对照品_中药对照品_标准品_对照药材_「格利普」高纯中药标准品厂家-成都格利普生物科技有限公司 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 美国PARKER齿轮泵,美国PARKER柱塞泵,美国PARKER叶片泵,美国PARKER电磁阀,美国PARKER比例阀-上海维特锐实业发展有限公司二部 | 厂房出租-厂房规划-食品技术-厂房设计-厂房装修-建筑施工-设备供应-设备求购-龙爪豆食品行业平台 | 武汉高低温试验箱_恒温恒湿试验箱厂家-武汉蓝锐环境科技有限公司 | 检验科改造施工_DSA手术室净化_导管室装修_成都特殊科室建设厂家_医疗净化工程公司_四川华锐 | SPC工作站-连杆综合检具-表盘气动量仪-内孔缺陷检测仪-杭州朗多检测仪器有限公司 | 网带通过式抛丸机,,网带式打砂机,吊钩式,抛丸机,中山抛丸机生产厂家,江门抛丸机,佛山吊钩式,东莞抛丸机,中山市泰达自动化设备有限公司 | 盘式曝气器-微孔曝气器-管式曝气器-曝气盘-斜管填料 | 郑州市前程水处理有限公司 | 北京律师事务所_房屋拆迁律师_24小时免费法律咨询_云合专业律师网 | 二手注塑机回收_旧注塑机回收_二手注塑机买卖 - 大鑫二手注塑机 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | 深圳宣传片制作_产品视频制作_深圳3D动画制作公司_深圳短视频拍摄-深圳市西典映画传媒有限公司 | 华溶溶出仪-Memmert稳定箱-上海协烁仪器科技有限公司 | 深圳市宏康仪器科技有限公司-模拟高空低压试验箱-高温防爆试验箱-温控短路试验箱【官网】 | 清管器,管道清管器,聚氨酯发泡球,清管球 - 承德嘉拓设备 | 真空包装机-诸城市坤泰食品机械有限公司| 数码听觉统合训练系统-儿童感觉-早期言语评估与训练系统-北京鑫泰盛世科技发展有限公司 | 建筑消防设施检测系统检测箱-电梯**检测仪器箱-北京宇成伟业科技有限责任公司 | 锥形螺带干燥机(新型耙式干燥机)百科-常州丰能干燥工程 | 818手游网_提供当下热门APP手游_最新手机游戏下载 | 信阳市建筑勘察设计研究院有限公司 | 立式硫化罐-劳保用品硫化罐-厂家直销-山东鑫泰鑫硫化罐厂家 |