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(使用線程逐塊處理文件)
主站蜘蛛池模板: 网站建设,北京网站建设,北京网站建设公司,网站系统开发,北京网站制作公司,响应式网站,做网站公司,海淀做网站,朝阳做网站,昌平做网站,建站公司 | 道达尔润滑油-食品级润滑油-道达尔导热油-合成导热油,深圳道达尔代理商合-深圳浩方正大官网 | 常州翔天实验仪器厂-恒温振荡器-台式恒温振荡器-微量血液离心机 恒温恒湿箱(药品/保健品/食品/半导体/细菌)-兰贝石(北京)科技有限公司 | 南京种植牙医院【官方挂号】_南京治疗种植牙医院那个好_南京看种植牙哪里好_南京茀莱堡口腔医院 尼龙PA610树脂,尼龙PA612树脂,尼龙PA1010树脂,透明尼龙-谷骐科技【官网】 | AGV无人叉车_激光叉车AGV_仓储AGV小车_AGV无人搬运车-南昌IKV机器人有限公司[官网] | 昆明挖掘机修理厂_挖掘机翻新再制造-昆明聚力工程机械维修有限公司 | 艾乐贝拉细胞研究中心 | 国家组织工程种子细胞库华南分库 | 精密五金加工厂-CNC数控车床加工_冲压件|蜗杆|螺杆加工「新锦泰」 | 纸张环压仪-纸张平滑度仪-杭州纸邦自动化技术有限公司 | 齿轮减速机_齿轮减速电机-VEMT蜗轮蜗杆减速机马达生产厂家瓦玛特传动瑞环机电 | 智能风向风速仪,风速告警仪,数字温湿仪,综合气象仪(气象五要素)-上海风云气象仪器有限公司 | HDPE土工膜,复合土工膜,防渗膜价格,土工膜厂家-山东新路通工程材料有限公司 | 外观设计_设备外观设计_外观设计公司_产品外观设计_机械设备外观设计_东莞工业设计公司-意品深蓝 | 蜜蜂职场文库_职场求职面试实用的范文资料大全 | 江西自考网-江西自学考试网 | 煤粉取样器-射油器-便携式等速飞灰取样器-连灵动 | 猪I型/II型胶原-五克隆合剂-细胞冻存培养基-北京博蕾德科技发展有限公司 | 盘式曝气器-微孔曝气器-管式曝气器-曝气盘-斜管填料 | 郑州市前程水处理有限公司 | PC构件-PC预制构件-构件设计-建筑预制构件-PC构件厂-锦萧新材料科技(浙江)股份有限公司 | ERP企业管理系统永久免费版_在线ERP系统_OA办公_云版软件官网 | 微信聊天记录恢复_手机短信删除怎么恢复_通讯录恢复软件下载-快易数据恢复 | 英国公司注册-新加坡公司注册-香港公司开户-离岸公司账户-杭州商标注册-杭州优创企业 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 搪玻璃冷凝器_厂家-越宏化工设备 | 包装盒厂家_纸盒印刷_礼品盒定制-济南恒印包装有限公司 | 【法利莱住人集装箱厂家】—活动集装箱房,集装箱租赁_大品牌,更放心 | 壹作文_中小学生优秀满分作文大全| 蓄电池在线监测系统|SF6在线监控泄露报警系统-武汉中电通电力设备有限公司 | 管家婆-管家婆软件-管家婆辉煌-管家婆进销存-管家婆工贸ERP | 东莞工厂厂房装修_无尘车间施工_钢结构工程安装-广东集景建筑装饰设计工程有限公司 | 板材品牌-中国胶合板行业十大品牌-环保板材-上海声达板材 | 保健品OEM贴牌代加工厂家_德州健之源| 板框压滤机-隔膜压滤机配件生产厂家-陕西华星佳洋装备制造有限公司 | 浙江红酒库-冰雕库-气调库-茶叶库安装-医药疫苗冷库-食品物流恒温恒湿车间-杭州领顺实业有限公司 | 沈阳网站建设_沈阳网站制作_沈阳网页设计-做网站就找示剑新零售 沈阳缠绕膜价格_沈阳拉伸膜厂家_沈阳缠绕膜厂家直销 | 3A别墅漆/3A环保漆_广东美涂士建材股份有限公司【官网】 | 品牌广告服务平台,好排名,好流量,好生意。 | 机床主轴维修|刀塔维修|C轴维修-常州翔高精密机械有限公司 | 网站建设,北京网站建设,北京网站建设公司,网站系统开发,北京网站制作公司,响应式网站,做网站公司,海淀做网站,朝阳做网站,昌平做网站,建站公司 | 亚克力制品定制,上海嘉定有机玻璃加工制作生产厂家—官网 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 |