【问题标题】:Make Android TextView invisible until button pressed?在按下按钮之前使Android TextView不可见?
【发布时间】:2013-11-10 18:21:24
【问题描述】:

我希望在按下按钮之前使 TextView 不可见。具体来说,它们是问题的答案,只有在按下它下方的按钮后才能看到。

我目前的情况>>

public class AndroidAssignment2_1 extends Activity {

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

        Button next = (Button) findViewById(R.id.QButton);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();

            }
        }); 
            Button next1 = (Button) findViewById(R.id.QButton);
            next1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_2.class);
                    startActivityForResult(myIntent, 0);
                }
            }); 


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.android_assignment2_1, menu);
        return true;
    }

}

这个类的xml

<LinearLayout 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" >

<TextView android:id="@+id/Questions"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:text="@string/Q2"   />

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

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

<TextView android:id="@+id/Answers"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:hint="@string/A2" />

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


</LinearLayout>

我认为不需要更多,我只是在寻找适用于“AButton”的代码,以使 TextView“Answers”只有在用户单击后才可见。

【问题讨论】:

    标签: java android android-layout


    【解决方案1】:

    XML:

    <TextView android:id="@+id/Answers"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:hint="@string/A2"
        android:visibility="invisible"/>
    

    代码:

    Button button = (Button) findViewById(R.id.AButton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            TextView tv = AndroidAssignment2_1.this.findViewById(R.id.Answers);
            tv.setVisibility(View.VISIBLE);
        }
    }); 
    

    【讨论】:

    • 在 TextView 中按 OK 时,我该怎么做才能让它再次变得不可见?
    • 在 TextView 中按 OK 是什么意思?你能解释更多吗?你的意思是按 textView 本身还是按同一个按钮或另一个按钮?
    • > 我按下按钮 > 文本视图出现 > 我完成输入我的文本 > 我按下 OK -- 我想要的是当我按下 OK 时文本视图再次消失!现在明白了吗?
    • 确定按钮是另一个按钮吗?我的意思是让textview首先出现的按钮还是另一个按钮?
    • 你按键盘上的 OK 来发送你的文本。
    【解决方案2】:

    为了让这件事起作用,首先我们需要制作一个 TextView,在其中写入我们的消息,如果 Textview 像这样,使用 visibility 属性使 TextView 不可见:

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            **android:visibility="invisible"**/>
    

    然后在您的主活动类中创建一个方法,使用 setVisibility() 方法在我们的布局中显示不可见的测试视图,如下所示:

      public void onLoveButtonClicked(View view){
            TextView textView = (TextView) findViewById(R.id.haikuTextView);
            textView.setVisibility(View.VISIBLE);
    
      }
    

    【讨论】:

      【解决方案3】:

      将 Answers TextView 的可见性默认设置为不可见或消失。

      以与查找其他 Button 视图相同的方式查找 AButton Button。

      在 AButton Button 上设置一个 onClickListener,就像其他按钮的侦听器一样,它会做两件事:

      以与查找 Button 视图相同的方式查找答案 TextView。

      在答案 TextView 上调用 setVisibility

      【讨论】:

      • setVisibility是放在layout下还是在onCreate方法中调用?
      • @Learning2Code 通常的方法是在布局中使用android:visibility 将可见性设置为“gone”,然后在onClickHandler 中使用setVisibility 使其可见。跨度>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 2016-12-31
      • 1970-01-01
      • 2017-06-07
      • 2021-09-16
      • 1970-01-01
      • 2015-08-17
      相关资源
      最近更新 更多