android studio OnClicklistener第一次点击没有反应 [英] android studio OnClicklistener no reaction on first Click

查看:384
本文介绍了android studio OnClicklistener第一次点击没有反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,尽管我在尝试时可以听到Click声音,但是我的OnClicklistener却没有获得第一声Click.我只是在寻找解决方案,但没有找到适合我的解决方案.

Hello my OnClicklistener does not get the first Click although i can hear the Click sound when i try it. I just searched for a solution but i have not found one that works for me.

XML

 <Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="140dp"
    android:layout_height="40dp"
    android:background="@drawable/button"
    android:text="@string/ButtonNO"
    android:onClick="KlickNo"
    android:id="@+id/NoKlickKopf"
    android:layout_toEndOf="@+id/imageView2"
    android:layout_below="@+id/imageView2"
    android:layout_alignLeft="@+id/button5"
    android:layout_alignStart="@+id/button5" />

Java

public void KlickYes(查看){

public void KlickYes(View view) {

    Button button1 = (Button) findViewById(R.id.YesKlickKopf);
    button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            frage = frage + 1;

            switch (frage) {

                case 1: //error

                    break;
                case 2: //error

                    break;
                default:
                    break;
            }
        }
    });

}

public void KlickNo (View view) {
    Button button2 = (Button) findViewById(R.id.NoKlickKopf);
    button2.setOnClickListener(new OnClickListener() {

        final TextView mTextView = (TextView) findViewById(R.id.textViewkopffrage);
        final TextView tTextView = (TextView) findViewById(R.id.texttrivakskopf);
        @Override
        public void onClick(View v) {
            frage = frage + 1;

            switch (frage) {

                case 1: //error


                    break;
                case 2: //error

                    break;
                default:
                    break;
            }
        }
    });

}

推荐答案

此代码中的操作是您首先通过xml设置onClickListener来运行方法void KlickNo (View view)

what you do in this code is that you first set an onClickListener through the xml to run the methodvoid KlickNo (View view)

,然后通过设置new OnClickListener(){ blah,blah}代替以前的设置来取消所有这些操作

and then you cancel all this by setting a new OnClickListener(){ blah,blah} replacing the previous setting

您必须使用其中之一. xml OnCLickListener设置的OnClick调用KlickNo方法,因此直接在其中进行操作并删除.setOnClickListener(.....)声明

You either have to use one or the other. The KlickNo method is called by the OnClick of the set by xml OnCLickListener, so do your stuff directly there and delete the .setOnClickListener(.....) declaration

这篇关于android studio OnClicklistener第一次点击没有反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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