项目点击的奇怪行为

时间:2017-03-20 20:26:03

标签: android user-interface onclick buttonclick

我正在构建一个发送USSD命令以检查余额等的应用程序。 当我尝试从项目点击启动活动时,应用程序很奇怪,这里是从图像按钮:我必须触摸两次才能启动活动。我不知道出了什么问题。

看看我的代码:

MainActivity.java:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;


public class MainActivity extends AppCompatActivity {

    ImageView accountView;
    ImageView activationsView;
    ImageView servicesView;
    ImageView othersView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

   public void openAccountView(View view){
        accountView = (ImageView) findViewById(R.id.accountView);
        accountView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, AccountActivity.class);
                startActivity(i);
            }
        });
    }

    public void openActivationsView(View view){
        activationsView = (ImageView) findViewById(R.id.activationsView);
        activationsView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, ActivationsActivity.class);
                startActivity(i);
            }
        });
    }

    public void openServicesView(View view){
        servicesView = (ImageView) findViewById(R.id.servicesView);
        servicesView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, ServicesActivity.class);
                startActivity(i);
            }
        });
    }

    public void openOthersView(View view){

    }


}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@id/layout_dashboard_parent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@color/c_dashboard_btn_quick_clean_color"
        android:orientation="horizontal">

        <Button
            android:id="@+id/buttonCB"
            style="@android:style/Widget.Material.Light.Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="2dip"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip"
            android:layout_weight="2"
            android:background="@color/c_main_back_ground_color"
            android:elevation="7dp"
            android:text="@string/check_balance"
            tools:ignore="ButtonStyle"
            tools:targetApi="lollipop" />

        <Button
            android:id="@+id/buttonLB"
            style="@android:style/Widget.Material.Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip"
            android:layout_marginStart="2dip"
            android:layout_weight="2"
            android:background="@color/c_main_back_ground_color"
            android:elevation="7dp"
            android:text="@string/load_balance"
            tools:ignore="ButtonStyle"
            tools:targetApi="lollipop" />
    </LinearLayout>

    <ScrollView
        android:id="@id/main_scroll_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/webview_ads"
        tools:ignore="UnknownIdInLayout">

        <LinearLayout
            android:id="@id/layout_dashboard_scroll_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1">

            <FrameLayout
                android:id="@id/dashboard_alert_section"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="gone">

                <include layout="@layout/dashboard_feature_tip_item" />

            </FrameLayout>

            <FrameLayout
                android:id="@id/main_control_layout"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="345">

                <FrameLayout
                    android:id="@id/dashboard_progress_bar"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginTop="@dimen/dashboard_clean_progess_mar_top"
                    android:elevation="@dimen/dashboard_progressbar_elevation"
                    android:visibility="gone"
                    tools:ignore="UselessParent"
                    tools:targetApi="lollipop">

                    <ImageView
                        android:layout_width="@dimen/dashboard_progress_clean_all_image_dimension"
                        android:layout_height="@dimen/dashboard_progress_clean_all_image_dimension"
                        android:layout_gravity="center"
                        android:contentDescription=""
                        android:src="@drawable/btn_ram_checkbox"
                        tools:ignore="ContentDescription" />

                    <ProgressBar
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center" />
                </FrameLayout>
            </FrameLayout>
        </LinearLayout>
    </ScrollView>

    <RelativeLayout
        android:id="@id/layout_main"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="@color/c_main_back_ground_color">

        <RelativeLayout
            android:id="@id/layout_account"
            android:layout_width="@dimen/dashboard_feature_layout_width"
            android:layout_height="@dimen/dashboard_feature_layout_height"
            android:layout_marginEnd="@dimen/dashboard_feature_middle_margin"
            android:layout_marginLeft="@dimen/dashboard_feature_leftmost_margin"
            android:layout_marginRight="@dimen/dashboard_feature_middle_margin"
            android:layout_marginStart="@dimen/dashboard_feature_leftmost_margin"
            android:layout_marginTop="@dimen/dashboard_feature_top_margin"
            android:background="@drawable/dashboard_bg_selector"
            android:clickable="false"
            android:elevation="6dp"
            android:focusable="false"
            tools:targetApi="lollipop">

            <ImageView
                android:id="@+id/accountView"
                android:layout_width="130dip"
                android:layout_height="130dip"
                android:layout_alignParentEnd="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:adjustViewBounds="false"
                android:background="@color/checkbox_button_color"
                android:clickable="true"
                android:contentDescription="@string/account"
                android:cropToPadding="false"
                android:onClick="openAccountView"
                android:visibility="visible"
                app:srcCompat="@drawable/account" />

            <TextView
                android:id="@+id/accountTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="14dp"
                android:gravity="center_vertical|center_horizontal|center"
                android:text="@string/account"
                android:textColor="@color/body_text_color"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/accountView"
                android:paddingBottom="2dip"
                android:paddingEnd="2dip"
                android:paddingLeft="2dip"
                android:paddingRight="2dip"
                android:paddingStart="2dip"
                android:text="@string/account_detail"
                android:textAlignment="center"
                android:textColor="@color/black"
                android:textSize="12sp" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@id/layout_activations"
            android:layout_width="@dimen/dashboard_feature_layout_width"
            android:layout_height="@dimen/dashboard_feature_layout_height"
            android:layout_marginEnd="@dimen/dashboard_feature_rightmost_margin"
            android:layout_marginLeft="@dimen/dashboard_feature_middle_margin"
            android:layout_marginRight="@dimen/dashboard_feature_rightmost_margin"
            android:layout_marginStart="@dimen/dashboard_feature_middle_margin"
            android:layout_marginTop="@dimen/dashboard_feature_top_margin"
            android:layout_toEndOf="@id/layout_account"
            android:layout_toRightOf="@id/layout_account"
            android:background="@drawable/dashboard_bg_selector"
            android:clickable="false"
            android:elevation="@dimen/dashboard_feature_bg_elevation"
            android:focusable="false"
            android:onClick="openActivationsView"
            tools:targetApi="lollipop">

            <ImageView
                android:id="@+id/activationsView"
                android:layout_width="130dip"
                android:layout_height="130dip"
                android:layout_alignParentEnd="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="false"
                android:layout_centerHorizontal="true"
                android:layout_centerInParent="true"
                android:layout_centerVertical="true"
                android:clickable="true"
                android:contentDescription="@string/activations_packs"
                android:onClick="openActivationsView"
                app:srcCompat="@drawable/activations" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/activationsView"
                android:elevation="7dp"
                android:paddingBottom="2dip"
                android:paddingEnd="2dip"
                android:paddingLeft="2dip"
                android:paddingRight="2dip"
                android:paddingStart="2dip"
                android:text="@string/activations_detail"
                android:textAlignment="center"
                android:textColor="@color/black"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/activationsTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="14dp"
                android:gravity="center_vertical|center_horizontal|center"
                android:text="@string/activations_packs"
                android:textAlignment="center"
                android:textColor="@color/black"
                android:textSize="18sp"
                android:textStyle="bold" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@id/layout_services"
            android:layout_width="@dimen/dashboard_feature_layout_width"
            android:layout_height="@dimen/dashboard_feature_layout_height"
            android:layout_below="@id/layout_account"
            android:layout_marginBottom="10dip"
            android:layout_marginEnd="@dimen/dashboard_feature_middle_margin"
            android:layout_marginLeft="@dimen/dashboard_feature_leftmost_margin"
            android:layout_marginRight="@dimen/dashboard_feature_middle_margin"
            android:layout_marginStart="@dimen/dashboard_feature_leftmost_margin"
            android:layout_marginTop="@dimen/dashboard_feature_ram_security_top_margin"
            android:background="@drawable/dashboard_bg_selector"
            android:clickable="false"
            android:elevation="5dp"
            android:focusable="false"
            android:onClick="openServicesView"
            tools:targetApi="lollipop">

            <TextView
                android:id="@+id/servicesTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="14dp"
                android:gravity="center_vertical|center_horizontal|center"
                android:text="@string/services"
                android:textColor="@android:color/black"
                android:textSize="18sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/servicesView"
                android:layout_width="130dip"
                android:layout_height="130dip"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/services"
                app:srcCompat="@drawable/services" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/servicesView"
                android:paddingBottom="2dip"
                android:paddingEnd="2dip"
                android:paddingLeft="2dip"
                android:paddingRight="2dip"
                android:paddingStart="2dip"
                android:text="@string/services_detail"
                android:textAlignment="center"
                android:textColor="@color/black"
                android:textSize="12sp" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@id/layout_others"
            android:layout_width="@dimen/dashboard_feature_layout_width"
            android:layout_height="@dimen/dashboard_feature_layout_height"
            android:layout_below="@id/layout_activations"
            android:layout_marginBottom="10dip"
            android:layout_marginEnd="@dimen/dashboard_feature_rightmost_margin"
            android:layout_marginLeft="@dimen/dashboard_feature_middle_margin"
            android:layout_marginRight="@dimen/dashboard_feature_rightmost_margin"
            android:layout_marginStart="@dimen/dashboard_feature_middle_margin"
            android:layout_marginTop="@dimen/dashboard_feature_ram_security_top_margin"
            android:layout_toEndOf="@id/layout_services"
            android:layout_toRightOf="@id/layout_services"
            android:background="@drawable/dashboard_bg_selector"
            android:clickable="false"
            android:elevation="@dimen/dashboard_feature_bg_elevation"
            android:focusable="false"
            tools:targetApi="lollipop">

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="31dp"
                android:text="@string/other_detail"
                android:textColor="@color/black"
                android:textSize="12sp" />

            <ImageView
                android:id="@+id/otherView"
                android:layout_width="130dip"
                android:layout_height="130dip"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/other"
                app:srcCompat="@drawable/help" />

            <TextView
                android:id="@+id/otherTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="14dp"
                android:clickable="true"
                android:gravity="center_vertical|center_horizontal|center"
                android:onClick="openOthersView"
                android:text="@string/other"
                android:textColor="@color/black"
                android:textSize="18sp"
                android:textStyle="bold" />

        </RelativeLayout>

    </RelativeLayout>


</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

您通过onClick imageView调用openAccountView方法:

android:onClick="openAccountView"

因此,无需将onClicklistener添加到您的imageView。

删除onClicklistener中的openAccountView,使用以下代码:

public void openAccountView(View view){
          Intent i = new Intent(MainActivity.this, AccountActivity.class);
          startActivity(i);
    }

希望这有帮助。