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(使用線程逐塊處理文件)
主站蜘蛛池模板: 熔体泵_熔体出料泵_高温熔体泵-郑州海科熔体泵有限公司 | 西安耀程造价培训机构_工程预算实训_广联达实作实操培训 | 存包柜厂家_电子存包柜_超市存包柜_超市电子存包柜_自动存包柜-洛阳中星 | 上海盐水喷雾试验机_两厢式冷热冲击试验箱-巨怡环试 | 液氮罐_液氮容器_自增压液氮罐_杜瓦瓶_班德液氮罐厂家 | 希望影视-高清影视vip热播电影电视剧免费在线抢先看 | 质构仪_鱼糜弹性仪-上海腾拔仪器科技有限公司 | 管家婆-管家婆软件-管家婆辉煌-管家婆进销存-管家婆工贸ERP | 太空舱_民宿太空舱厂家_移动房屋太空舱价格-豪品建筑 | 钢绞线万能材料试验机-全自动恒应力两用机-混凝土恒应力压力试验机-北京科达京威科技发展有限公司 | 全自动真空上料机_粉末真空上料机_气动真空上料机-南京奥威环保科技设备有限公司 | 蒸汽热收缩机_蒸汽发生器_塑封机_包膜机_封切收缩机_热收缩包装机_真空机_全自动打包机_捆扎机_封箱机-东莞市中堡智能科技有限公司 | 自动化展_机器人展_机床展_工业互联网展_广东佛山工博会 | 上海公众号开发-公众号代运营公司-做公众号的公司企业服务商-咏熠软件 | 高压包-点火器-高压发生器-点火变压器-江苏天网 | 北京包装设计_标志设计公司_包装设计公司-北京思逸品牌设计 | 订做不锈钢_不锈钢定做加工厂_不锈钢非标定制-重庆侨峰金属加工厂 | 执业药师报名时间,报考条件,考试时间-首页入口 | 天津市能谱科技有限公司-专业的红外光谱仪_红外测油仪_紫外测油仪_红外制样附件_傅里叶红外光谱技术生产服务厂商 | 磁粉制动器|张力控制器|气胀轴|伺服纠偏控制器整套厂家--台灵机电官网 | 黑田精工电磁阀-CAMMOZI气缸-ROSS电磁-上海茂硕机械设备有限公司 | 干培两用箱-细菌恒温培养箱-菲斯福仪器| 美能达分光测色仪_爱色丽分光测色仪-苏州方特电子科技有限公司 | 多功能三相相位伏安表-变压器短路阻抗测试仪-上海妙定电气 | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | 致胜管家软件服务【在线免费体验】 | TwistDx恒温扩增-RAA等温-Jackson抗体-默瑞(上海)生物科技有限公司 | 直齿驱动-新型回转驱动和回转支承解决方案提供商-不二传动 | 油液红外光谱仪-油液监测系统-燃油嗅探仪-上海冉超光电科技有限公司 | 课件导航网_ppt课件_课件模板_课件下载_最新课件资源分享发布平台 | SOUNDWELL 编码器|电位器|旋转编码器|可调电位器|编码开关厂家-广东升威电子制品有限公司 | 中央空调温控器_风机盘管温控器_智能_液晶_三速开关面板-中央空调温控器厂家 | 德州网站开发定制-小程序开发制作-APP软件开发-「两山开发」 | 工作服定制,工作服定做,工作服厂家-卡珀职业服装(苏州)有限公司 | 铸铝门厂家,别墅大门庭院大门,别墅铸铝门铜门[十大品牌厂家]军强门业 | 硬度计,金相磨抛机_厂家-莱州华煜众信试验仪器有限公司 | 代做标书-代写标书-专业标书文件编辑-「深圳卓越创兴公司」 | 真空泵维修保养,普发,阿尔卡特,荏原,卡西亚玛,莱宝,爱德华干式螺杆真空泵维修-东莞比其尔真空机电设备有限公司 | 电抗器-能曼电气-电抗器专业制造商 | 干式磁选机_湿式磁选机_粉体除铁器-潍坊国铭矿山设备有限公司 | 拉力测试机|材料拉伸试验机|电子拉力机价格|万能试验机厂家|苏州皖仪实验仪器有限公司 |