Android的按钮总是两次点击射击的onClick() [英] Android Button always takes two clicks to fire onClick()

查看:250
本文介绍了Android的按钮总是两次点击射击的onClick()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RelativeLayout的,它包含一个按钮和一些TextViews和EditTexts滚动型的内部。

在我的XML布局文件,我定义安卓的onClick但它总是需要的按钮两次点击触发事件。该按钮始终获得焦点的第一个点击触发第二次点击onClick事件。我已经尝试设置可聚焦和focusableInTouchMode都为假,但行为不会改变。

下面是我的布局文件:

 <滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    工具:上下文=DensityActivity。>    < RelativeLayout的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT>        <的TextView
        ...        <的TextView
        ...        <的TextView
        ...        <的EditText
        ...        <按钮
            机器人:ID =@ + ID / ad_button_calculate
            机器人:layout_width =112dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ ID / ad_edit_obs_temp
            机器人:layout_alignParentRight =真
            机器人:layout_marginTop =20pt
            机器人:paddingLeft =6PT
            安卓的onClick =的onClick
            机器人:可聚焦=假
            机器人:focusableInTouchMode =假
            机器人:文字=@字符串/ button_calculate/>        <的TextView
        ...    < / RelativeLayout的>
< /滚动型>

任何意见或建议,为什么在可聚焦和focusableInTouchMode似乎并没有做什么?

我想这可能是我的onClick()方法没有做它应该让我把它降低到简单的东西只是为了看看它的表现是一样的。这是我简单的onClick():

 公共无效的onClick(查看视图){    新AlertDialog.Builder(本).setTitle(哎呀)setMessage(当心!)setNeutralButton(关闭,NULL).show();}


解决方案

OK,我发现它。那是当然,我自己的错。
在我的onCreate方法的末尾我这样做:

  //将焦点设置到计算按钮使键盘不会出现自动向上
按钮calcButton =(按钮)findViewById(R.id.ac_button_calculate);
calcButton.setFocusable(真);
calcButton.setFocusableInTouchMode(真);
calcButton.requestFocus();

所以,当然,不管我在XML文件中那样,我重写它在我的code。
相反,我用这个隐藏键盘:

  getWindow()setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)。

伟大的工程。

I have a RelativeLayout inside of a ScrollView that contains a Button and some TextViews and EditTexts.

In my xml layout file, I am defining android:onClick but it always takes two clicks of the button to fire the event. The button always gets the focus on the first click and fires the onClick event on the second click. I have tried setting focusable and focusableInTouchMode both to false but the behavior doesn't change.

Here is my layout file:

<ScrollView 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".DensityActivity" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
        ...

        <TextView
        ...

        <TextView
        ...

        <EditText
        ...

        <Button
            android:id="@+id/ad_button_calculate"
            android:layout_width="112dp"
            android:layout_height="wrap_content"
            android:layout_below="@id/ad_edit_obs_temp"
            android:layout_alignParentRight="true"
            android:layout_marginTop="20pt"
            android:paddingLeft="6pt"
            android:onClick="onClick"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="@string/button_calculate" />

        <TextView
        ...

    </RelativeLayout>
</ScrollView>

Any ideas or suggestions as to why the focusable and focusableInTouchMode don't seem to do anything?

I thought it might be my onClick() method not doing what it should so I reduced it to something simple just to see and it behaves the same. Here is my simplified onClick():

public void onClick(View view) {

    new AlertDialog.Builder(this).setTitle("Argh").setMessage("Watch out!").setNeutralButton("Close", null).show();

}

解决方案

OK, I found it. It was, of course, my own mistake. At the end of my onCreate method I was doing this:

// Set the focus to the calculate button so the keyboard won't show up automatically 
Button calcButton = (Button)findViewById( R.id.ac_button_calculate );
calcButton.setFocusable( true );
calcButton.setFocusableInTouchMode( true ); 
calcButton.requestFocus();

So of course, no matter what I did in my xml file, I was overriding it in my code. Instead, I used this to hide the keyboard:

getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN );

Which works great.

这篇关于Android的按钮总是两次点击射击的onClick()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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