【问题标题】:Android - Button must clicked twice to workAndroid - 按钮必须点击两次才能工作
【发布时间】:2012-10-12 14:00:16
【问题描述】:

我有一个带有一些按钮的栏和下面的 web 视图的活动。 每个按钮都关联一个 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>

这是onClickListener

private void initButton(){
    bottoni = new Button[6];
    bottoni[0] = (Button)findViewById(R.id.db1);
    bottoni[1] = (Button)findViewById(R.id.db2);
    bottoni[2] = (Button)findViewById(R.id.db3);
    bottoni[3] = (Button)findViewById(R.id.db4);
    bottoni[4] = (Button)findViewById(R.id.db5);
    bottoni[5] = (Button)findViewById(R.id.db6);
    for(int i=0;i<6;i++)
        setListB(bottoni[i],buttons[i]);
    }
}

private void setListB(Button b,String content_b){
    String[] all = content_b.split("@@");
    final String title = all[0];
    final String link = all[1];
    b.setText(title);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            web.loadUrl(link);
        }
    });
}

我该如何解决?谢谢!!!

【问题讨论】:

  • 你在哪里initButton()
  • 在活动的“onCreate”结束时
  • 这不是您问题的答案,但出于性能原因,您不应创建多个新的 OnClickListener,您只能有 1 个侦听器并使用 switch(v.getId()) 来比较哪个按钮是点击并采取相应行动。

标签: android button onclicklistener


【解决方案1】:

尝试更改方法名称(可能会混淆编译器)并在使用多个按钮时使用 switch。不确定方法名,试试看吧。

【讨论】:

    【解决方案2】:

    在类级别创建一个clickedButton 变量

    View 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;
            }
        }
    
    }
    

    祝你好运:)

    【讨论】:

    • onClick 并非所有按钮都通用.. 如您所见,我循环遍历所有按钮设置 onclick 为每个按钮
    • scuse me.. 但是您的登录与我的问题相反.. 在您的方式中,我必须单击两次才能加载 URL.. 我需要单击一次才能加载..
    • 我认为即使onClick 不常见,它也应该可以工作,因为单击的按钮将用作所有按钮之间的公共变量;)
    • 你能解释一下你到底想要什么吗?我的理解是您需要通过单击按钮两次在WebView 中打开URL
    • 将所有按钮添加到另一个视图,如 ListView。然后列表视图将获得第一次点击,内部按钮将获得下一次(第二次)点击。所以最终您的 webview 将在第二次点击时加载 url。
    猜你喜欢
    • 2012-10-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    相关资源
    最近更新 更多