首页 > 解决方案 > 如何根据android中的输入使视图不可见

问题描述

我仍然是 android 的初学者,正在学习活动和意图,然后正在创建一个小的消息回复应用程序。我想让这个文本字段在开始时不可见 activity_main.xml

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:text="@string/string_send"
        android:layout_alignParentRight="true"
        android:background="@color/red_color"
        android:textColor="@android:color/white"
        android:onClick="launchSecondActivity"
        />

    <EditText
        android:id="@+id/editText_Message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text"
        android:layout_alignParentBottom="true"
        android:text="@string/message" />

    <TextView
        android:id="@+id/textView_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:text="Reply Received" />

    <TextView
        android:id="@+id/textView_reply"
        android:layout_width="wrap_content"
        android:layout_below="@+id/textView_header"
        android:layout_height="wrap_content"
        android:text="This is the reply" />
</RelativeLayout>

这是我的 MainActivity 以及我想知道如何编写 if 语句的地方

public class MainActivity extends AppCompatActivity {
    //Declare the edit text view
    private EditText messageEditText;
    //Declare and initialize the key
    public static final String EXTRA_MESSAGE = "My message";
    //Declare the reply textView
    private TextView replyTextView;
    private TextView replyHeader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Initialize the edit text
        messageEditText = findViewById(R.id.editText_Message);
        //Initialize the reply textView
        replyTextView = findViewById(R.id.textView_reply);
        //Create an intent that gets an intent
        Intent myIntent = getIntent();
        //Make reply invisible at run time
        replyHeader = findViewById(R.id.textView_header);
        replyHeader.setVisibility(View.GONE);
        //Get user's Extra reply
        String myReply = myIntent.getStringExtra(SecondActivity.EXTRA_REPLY);
        //Set the message into the string
        replyTextView.setText(myReply);
    }

    public void launchSecondActivity(View view) {
        //create an intent
        Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
        //Get the user's message
        String message = messageEditText.getText().toString();
        //Put the user's message into the intent object
        myIntent.putExtra(EXTRA_MESSAGE, message);
        //Start Activity
        startActivity(myIntent);
    }

欢迎任何帮助

标签: android

解决方案


例如,您希望从一开始就使 textView_reply 不可见,但是在单击replyHeader 之后您想更改它的可见性,所以。在 xml 中更改您的 textView_reply,例如:

`

<TextView
            android:id="@+id/textView_reply"
            android:visibility="gone"
            android:layout_width="wrap_content"
            android:layout_below="@+id/textView_header"
            android:layout_height="wrap_content"
            android:text="This is the reply" />

`

当您需要使其可见时 - 只需调用:

replyHeader = findViewById(R.id.textView_header);
    replyHeader.setVisibility(View.Visible)

它适用于两种可见性类型


推荐阅读