android View.Onclick不会触发

时间:2016-11-24 20:05:09

标签: android xml android-layout android-studio android-fragments

问题是,3个图像视图中没有显示吐司,我也尝试记录任何文本在其onlick和匿名也没有任何建议吗? Screenshot

我的XML

<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/bg_login"
    android:orientation="vertical"
    android:paddingBottom="19dp"
    android:paddingLeft="10dp"
    android:paddingRight="9dp"
    tools:context="com.example.mahmoud.blink.LoginFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="217dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="51dp"></LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="5">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"></LinearLayout>

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:src="@mipmap/logo_small" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"></LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:background="@mipmap/overlay_white"
            android:clickable="false"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp">


                <ImageView
                    android:id="@+id/iv_tm"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:clickable="true"
                    app:srcCompat="@mipmap/t" />

                <ImageView
                    android:id="@+id/iv_fbm"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginEnd="31dp"
                    android:layout_marginRight="31dp"
                    android:layout_toLeftOf="@+id/iv_tm"
                    android:layout_toStartOf="@+id/iv_tm"
                    android:background="@mipmap/f"
                    android:clickable="true" />

                <ImageView
                    android:id="@+id/iv_googlem"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="31dp"
                    android:layout_marginStart="31dp"
                    android:layout_toEndOf="@+id/iv_tm"
                    android:layout_toRightOf="@+id/iv_tm"
                    android:clickable="true"
                    app:srcCompat="@mipmap/g" />
            </RelativeLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <EditText
                            android:id="@+id/txtEmail"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"

                            android:layout_marginLeft="10.5dp"
                            android:layout_marginRight="12dp"

                            android:drawableLeft="@mipmap/mail2"
                            android:drawablePadding="10dp"
                            android:hint="Email"
                            android:inputType="textEmailAddress" />

                        <EditText
                            android:id="@+id/txtPassword"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10.5dp"
                            android:layout_marginRight="12dp"
                            android:drawableLeft="@mipmap/password"
                            android:drawablePadding="10dp"
                            android:ems="10"
                            android:hint="Password"
                            android:inputType="textPassword" />
                    </LinearLayout>

                    <com.wang.avi.AVLoadingIndicatorView
                        android:id="@+id/avi"
                        style="@style/AVLoadingIndicatorView.Large"
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_gravity="center_vertical|center_horizontal"
                        app:indicatorColor="@color/colorPrimary"
                        app:indicatorName="BallClipRotatePulseIndicator" />
                </FrameLayout>

                <Button
                    android:id="@+id/btn_login"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="6dp"
                    android:layout_marginRight="6dp"
                    android:layout_marginTop="15dp"
                    android:background="@mipmap/normal_btn"
                    android:text="Sign In"
                    android:textColor="#8b572a" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:weightSum="2">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="6dp"
                        android:layout_marginRight="4dp"
                        android:layout_weight="1"
                        android:orientation="horizontal">

                        <Button
                            android:id="@+id/btn_forget_pass"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@mipmap/sencondry_btn"
                            android:text="Forgot Password"
                            android:textColor="#fff" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="4dp"
                        android:layout_marginRight="6dp"
                        android:layout_weight="1"
                        android:orientation="horizontal">

                        <Button
                            android:id="@+id/btn_signup"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@mipmap/sencondry_btn"
                            android:text="Sign up"
                            android:textColor="#fff" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Java代码

LoginFragment.java中的

 //decleration within the fragment class 
    ImageView iv_g;
    ImageView iv_fb;
    ImageView iv_t;

来自onCreateView方法

    View view = inflater.inflate(R.layout.fragment_login, container, false);
    iv_fb = (ImageView) view.findViewById(R.id.iv_fbm);
    iv_t = (ImageView) view.findViewById(R.id.iv_tm);
    iv_g = (ImageView) view.findViewById(R.id.iv_googlem);
    iv_fb.setOnClickListener(this);
    iv_g.setOnClickListener(this);
    iv_t.setOnClickListener(this);

来自onClick方法

      if (v.getId() == R.id.iv_googlem) {
            Toast.makeText(context, "google", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(context, MyService.class);
            intent.putExtra("type", "google");
            context.startService(intent);
        } else if (v.getId() == R.id.iv_fbm) {
            Toast.makeText(context, "facebook", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(context, MyService.class);
            intent.putExtra("type", "facebook");
            context.startService(intent);
        } else if (v.getId() == R.id.iv_tm) {
            Toast.makeText(context, "twitter", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(context, MyService.class);
            intent.putExtra("type", "twitter");
            context.startService(intent);
        }

3个按钮正常工作,但点击时不会触发3个ImageView

1 个答案:

答案 0 :(得分:0)

尝试使用此语法,看看它是否有效:

iv_fb.setOnClickListener(new view.OnClickListener{    public void onClick()
    {
          Toast.makeText(context, "google", Toast.LENGTH_SHORT).show();
          Intent intent = new Intent(context, MyService.class);
          intent.putExtra("type", "google");
          context.startService(intent);
    } });