简体   繁体   English

线性布局不可见OnClickListener

[英]Linear layout not getting visible OnClickListener

I have two layouts. 我有两种布局。 I want to keep one layout gone when the activity is loaded and it should be visible onClick of another layout. 我想让一个布局在活动加载时消失,它应该在另一布局的onClick上可见。 so I have added this code OnClickListener of the layout. 所以我添加了布局的代码OnClickListener。

 additionalContactFrom.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(linearLayoutFrom.getVisibility() == View.GONE){

                linearLayoutFrom.setVisibility(View.VISIBLE);

            }else{
                linearLayoutFrom.setVisibility(View.GONE);

            }
        }
    });

And have set visibility of the layout gone in xml file.. 并在xml文件中设置了布局的可见性。

<LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/LinearLayoutAdditionalContactFrom">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:id="@+id/imageView13"
                    android:layout_marginLeft="20dp"
                    android:background="@drawable/ic_person_black_48dp"
                    android:layout_marginTop="05dp"
                    />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/editText"
                    android:layout_marginRight="10dp"
                    android:drawableRight="@drawable/ic_expand_more_black_24dp"
                    android:text="Additional contact (optional)"
                    android:cursorVisible="false"
                    />
            </LinearLayout>
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="60dp"
                    android:layout_marginRight="50dp"
                    android:layout_gravity="center"
                    android:visibility="gone"
                    android:id="@+id/LinearLayoutFrom">

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/editText2"
                        android:layout_weight="1"
                        android:hint="Name"
                        android:layout_gravity="center"
                        />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/editText3"
                        android:layout_weight="1"
                        android:hint="Phone"
                        android:layout_gravity="center"
                       />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="OR"
                        android:id="@+id/textView19"
                        android:layout_gravity="center" />

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/textView13"
                android:layout_marginLeft="48dp"
                android:hint="Input if you're not receiver" />

        </LinearLayout>

Unable to understand whats going wrong.. The listener is not getting called at all.. Please help.. 无法理解发生了什么问题。.根本没有呼叫听众。.请帮助。

Change 更改

linearLayoutFrom.setVisibility(View.VISIBLE);

To

findViewById(R.id.YOURLAYOUTID).setVisibility(View.VISIBLE);

I think you can't access to local vars in sub methods 我认为您无法在子方法中访问本地变量

It Seems That Layout Visibility is Already set to GONE, so Onlick listener is not working on hidden View and it should not work too. 似乎“布局可见性”已设置为“消失”,因此Onlick侦听器无法在隐藏的View上工作,因此也不应工作。

INVISBLE means you are trying to add a listener to a view which is not there. INVISBLE表示您正在尝试将侦听器添加到不存在的视图中。 You can add the listener to a visible view only. 您只能将侦听器添加到可见视图。

WORKAROUND 解决方法

1) Try to make a dummy view which is visible but having the same color as background. 1)尝试制作一个可见的虚拟视图,但其颜色与背景相同。

2) Try to set the listener for parent and check the position (whether the position does belongs to INVISIBLE view). 2)尝试为父对象设置侦听器并检查位置(该位置是否确实属于INVISIBLE视图)。

Your problem is that your EditText is capturing the click event when you click on it. 您的问题是您的EditText在单击时捕获了click事件。 If you click somewhere else in the LinearLayout it should work. 如果您在LinearLayout中的其他位置单击,它将正常工作。 You can replace the EditText with a TextView if you don't need the user to edit the content. 如果不需要用户编辑内容,则可以用TextView替换EditText。

Please try to set the onClickListener to the EditText and to the ImageView and no to the LinearLayout 请尝试将onClickListener设置为EditText和ImageView,将no设置为LinearLayout

The problem is that the Handler of the EditText is most important that the LinearLayout handler. 问题在于EditText的处理程序比LinearLayout处理程序最重要。

Almost you can try to make a break point to the OnClick and see what is happend 几乎您可以尝试在OnClick上断点,看看发生了什么

This is an example to explain you how to do that: in MainActivity Class: 这是一个解释您如何执行此操作的示例:在MainActivity类中:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private LinearLayout linearLayoutFrom;
    private LinearLayout additionalContactFrom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        additionalContactFrom = (LinearLayout) findViewById(R.id.LinearLayoutAdditionalContactFrom);
        linearLayoutFrom = (LinearLayout) findViewById(R.id.LinearLayoutFrom);

        linearLayoutFrom.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),
                        "linearLayoutFrom clicked!!!", Toast.LENGTH_SHORT)
                        .show();
                if (additionalContactFrom.getVisibility() == View.GONE) {

                    additionalContactFrom.setVisibility(View.VISIBLE);

                } else {
                    additionalContactFrom.setVisibility(View.GONE);

                }
            }
        });

        additionalContactFrom.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),
                        "additionalContactFrom clicked!!!", Toast.LENGTH_SHORT)
                        .show();

                if (linearLayoutFrom.getVisibility() == View.GONE) {

                    linearLayoutFrom.setVisibility(View.VISIBLE);

                } else {

                    linearLayoutFrom.setVisibility(View.GONE);

                }
            }
        });
    }

}

in xml file: 在xml文件中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayoutAdditionalContactFrom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_dark"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayoutFrom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="50dp"
        android:background="@android:color/holo_green_light"
        android:orientation="vertical"
        android:visibility="gone" >
    </LinearLayout>

    <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="48dp"
        android:hint="Input if you&apos;re not receiver"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</FrameLayout>

This is very important that when you want add some view (for example add a linearlayout to another linerlayout). 这一点非常重要 ,当您要添加一些视图时(例如,将线性布局添加到另一个线性布局)。 you should use framelayout or relativelayout(do not use linearlayout) for do that. 您应该使用framelayout或relativelayout(不要使用linearlayout)来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM