Android 安卓-按钮必须点击两次才能工作

Android 安卓-按钮必须点击两次才能工作,android,button,onclicklistener,Android,Button,Onclicklistener,我有一个活动,其中有一个带有按钮的栏,下面有一个网络视图。 每个按钮都关联一个url,我执行onClickListener来加载webview中关联的url 我的问题是,在webview加载url之前,必须单击两次按钮 这是我的布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android

我有一个活动,其中有一个带有按钮的栏,下面有一个网络视图。 每个按钮都关联一个url,我执行onClickListener来加载webview中关联的url

我的问题是,在webview加载url之前,必须单击两次按钮

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:weightSum="1">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".005"
        android:background="@drawable/gradient"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
        >

            <Button
                android:id="@+id/db1"
                android:layout_width="140dp"
                android:layout_height="60dp"
                android:layout_marginLeft="100dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/transparent"
                android:text="VETRINA"
                android:focusableInTouchMode="false"
                android:textColor="@color/silver"
                android:textSize="20dp" />

            <Button
                android:id="@+id/db2"
                android:focusableInTouchMode="false"
                android:layout_width="140dp"
                android:layout_height="60dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/transparent"
                android:text="ANNUNCI"
                android:textColor="@color/silver"
                android:textSize="20dp" />

            <Button
                android:id="@+id/db3"
                android:layout_width="140dp"
                android:layout_height="60dp"
                android:layout_marginTop="10dp"
                android:focusableInTouchMode="false"
                android:background="@android:color/transparent"
                android:text="NOTIZIE"
                android:textColor="@color/silver"
                android:textSize="20dp" />

            <Button
                android:id="@+id/db4"
                android:layout_width="140dp"
                android:layout_height="60dp"
                android:focusableInTouchMode="false"
                android:layout_marginTop="10dp"
                android:background="@android:color/transparent"
                android:text="UTILI"
                android:textColor="@color/silver"
                android:textSize="20dp" />

            <Button
                android:id="@+id/db5"
                android:layout_width="140dp"
                android:layout_height="60dp"
                android:layout_marginTop="10dp"

                android:background="@drawable/round_corner"
                android:text="YOUTUBE"
                android:focusableInTouchMode="false"
                android:textColor="@color/silver"
                android:textSize="20dp" />
            <Button
                android:id="@+id/db6"
                android:layout_width="140dp"
                android:layout_height="60dp"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:layout_marginTop="10dp"
                android:background="@android:color/transparent"
                android:text="ANNUNCI"
                android:textColor="@color/silver"
                android:textSize="20dp" 
                />

        </LinearLayout>

    </LinearLayout>

    <com.pack.MyWebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".995" />

</LinearLayout>
我怎样才能修好它?谢谢

在类级别创建clickedButton变量

查看点击按钮

onClick如下所示,我假设onClick对于所有按钮都是通用的

public void onClick(View v) {

    if (clickedButton == null) {
        clickedButton = v;
    } else {
        if (clickedButton == v) {

            // load url and set clicked button to null
            loadUrl();
            clickedButton = null;
        } else {
            clickedButton = v;
        }
    }

}

祝您好运:

尝试更改方法名称,可能会混淆编译器,并在使用多个按钮时使用开关。不确定方法名称,请试一试。

您在哪里执行initButton?在“onCreate”活动的末尾,这不是您问题的答案,但出于性能原因,您不应该创建几个新的onClickListener,您只能有一个侦听器,并使用switchv.getId来比较单击的按钮并相应地执行操作。onClick不是所有按钮都通用的。。正如你所看到的,我循环trought所有按钮,为每个按钮设置onclick,请点击我。。但是你的登录与我的问题正好相反。。以您的方式,我必须单击两次以加载URL。。我需要单击一次来加载..我认为即使onClick不常见,它也应该工作,因为clicked按钮将用作所有按钮之间的公共变量;你能解释一下你到底想要什么吗?我的理解是,您需要在Web视图中单击两次按钮来打开URL。将所有按钮添加到另一个视图(如ListView)中。然后ListView将获得第一次单击,内部按钮将获得第二次单击。因此,最终您的Web视图将在第二次单击时加载URL。
public void onClick(View v) {

    if (clickedButton == null) {
        clickedButton = v;
    } else {
        if (clickedButton == v) {

            // load url and set clicked button to null
            loadUrl();
            clickedButton = null;
        } else {
            clickedButton = v;
        }
    }

}