【问题标题】:Handle Onclick event with a view visibility gone/visible处理视图可见性消失/可见的 Onclick 事件
【发布时间】:2015-04-17 10:23:45
【问题描述】:

我解释我的问题。

我有一个视图,其中有两个按钮和两个editText。 如果我触摸 button1,我想显示 edidText1。 如果我触摸button2,我想显示edidText2。

在我的布局中,edidText1 的可见性可见,edidText2 的可见性消失了。

My_Layout.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_gravity="center"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/btn_comments1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/form_comments"/>

        <Button
            android:id="@+id/btn_comments2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>
    <EditText
        android:id="@+id/et_comments1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:gravity="top" />


    <EditText
        android:id="@+id/et_comments2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:gravity="top" />

</LinearLayout>

在我的 onCreate 函数中

final EditText etComments1 = (EditText) alertDialogView.findViewById(R.id.et_comments);
etComments1.setText(comment1);
etComments1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        Toast.makeText(context, "TODO_1", Toast.LENGTH_LONG).show();
    }
});

final EditText etComments2 = (EditText) alertDialogView.findViewById(R.id.et_mes_corr);
etComments2.setText(comment2);
etComments2.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        Toast.makeText(context, "TODO_2", Toast.LENGTH_LONG).show();
    }
});


final Button btnComments1 = (Button) alertDialogView.findViewById(R.id.btn_comments1);
final Button btnComments2 = (Button) alertDialogView.findViewById(R.id.btn_comments2);

btnComments2.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        etComments1.setVisibility(View.INVISIBLE);
        etComments2.setVisibility(View.VISIBLE);
    }
});

btnComments1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        etComments1.setVisibility(View.VISIBLE);
        etComments2.setVisibility(View.INVISIBLE);
    }
});

当第一次点击我的 etComments1 时,TODO_1 被调用。 在我单击我的 btnComments2 以显示 etComments2 后,当我单击它时,不会调用 TODO_2。 在我点击我的 btnComments1 显示 etComments1 后,当我点击它时,TODO_1 没有被调用。

所以我在想,可以来view.Gone。所以我也在按钮的onClick中实现了这个事件,但问题还是一样。

有人可以帮助我吗?

【问题讨论】:

  • 你怎么知道它没有被调用?这只是一个注释,所以没有代码可以在那里运行。您究竟希望发生什么?为什么要点击EditText 视图?
  • 我没有把应该调用的确切代码放在函数中。
  • 尝试将android:clickable="true" 添加到您的EditTexts。你可能还需要android:editable="false"
  • 我尝试使用 android:clickable="true" 但这会改变任何事情。加了android:editable="false",还是不行,但是好一点,要调用onclick,我需要点击两次。
  • 已经有一段时间了,不太乐观,但你也可以试试android:focusableInTouchMode="false"。你能解释一下你想要发生的事情吗?可能有更好的方法

标签: android view visibility


【解决方案1】:

添加android:focusable="false"android:clickable="true" 后,我认为问题在于EditText 正在获得第一次点击的关注,然后第二次点击将调用监听器。

尝试添加

android:focusableInTouchMode="false"

到您的EditTexts,这应该允许单击来呼叫侦听器。

【讨论】:

    【解决方案2】:

    编辑

    问题出在布局文件和代码中。您已经使用了 visibility:gone 来表示 edittext2,如果您阅读了 doc,您就会明白“gone”的意思是“完全隐藏,就像没有添加视图一样”。

    因此,如果您想保留视图,则不应使用“gone”属性。另外,为什么您将变量声明为最终变量?这不是必需的。希望对你有帮助:)

    布局:

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <EditText
        android:id="@+id/et_comments1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:gravity="top" />
    
    
    <EditText
        android:id="@+id/et_comments2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:gravity="top" />
    
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_gravity="center"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/btn_comments1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button 1"/>
    
        <Button
            android:text="button 2"
            android:id="@+id/btn_comments2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

    主活动:

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    /**
     * Created by sonnet on 2/17/15.
     */
    public class MainActivity extends Activity {
    
    Button button1;
    Button button2;
    
    EditText editText1;
    EditText editText2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        button1 = (Button) findViewById(R.id.btn_comments1);
        button2 = (Button) findViewById(R.id.btn_comments2);
    
        editText1 = (EditText) findViewById(R.id.et_comments1);
        editText2 = (EditText) findViewById(R.id.et_comments2);
    
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText1.setVisibility(View.VISIBLE);
                editText2.setVisibility(View.INVISIBLE);
    
                int height = 0;
                int width = 0;
    
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
                editText2.setLayoutParams(params);
    
                height = FrameLayout.LayoutParams.WRAP_CONTENT;
                width = FrameLayout.LayoutParams.MATCH_PARENT;
    
                params = new LinearLayout.LayoutParams(width, height);
                editText1.setLayoutParams(params);
            }
        });
    
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText1.setVisibility(View.INVISIBLE);
                editText2.setVisibility(View.VISIBLE);
    
                int height = 0;
                int width = 0;
    
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
                editText1.setLayoutParams(params);
    
                height = FrameLayout.LayoutParams.WRAP_CONTENT;
                width = FrameLayout.LayoutParams.MATCH_PARENT;
    
                params = new LinearLayout.LayoutParams(width, height);
                editText2.setLayoutParams(params);
    
            }
        });
    
    
        editText1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "edittext1 is clicked", Toast.LENGTH_SHORT).show();
            }
        });
    
        editText2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "edittext2 is clicked", Toast.LENGTH_SHORT).show();
            }
        });
    
    }
    }
    

    【讨论】:

    • 关于 SO 的纯代码答案很少需要。请考虑添加关于您所做更改以及为什么它应该适用于 OP 的解释
    • 当我设置 view.INVISIBLE 时,我的活动没有像我需要的那样显示。我需要它完全隐藏。我真的需要一个或另一个在同一个地方,这取决于用户想要的开关之一。在您的回答中,这并没有解释我如何在 EditText 上设置 onClickListen。此外,我使用 final 是为了不声明为类变量,因为此布局在列表视图中使用,因此设置了多次。
    猜你喜欢
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 2020-02-10
    • 2015-12-12
    • 2014-10-05
    • 2020-10-27
    相关资源
    最近更新 更多