我有一个简单的布局,包括2个textview和2个edittexts,以及一对旋转器。我的问题是,根据第一个微调器的值,我想显示或隐藏某些视图或更改某些标签上的文本。我的实现工作正常,只要每当调用setVisible(View.INVISIBLE)时,edittext和textview实际上不会消失,直到我点击屏幕上的其他内容。一旦我点击,例如,我没有隐藏的编辑文本,我想隐藏的视图将继续并消失。我曾尝试在他们身上调用invalidate(),因为我已在其他帖子中看到刷新视图但它无效。我已经发布了下面的XML布局和相关的java代码。所有帮助表示赞赏。
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.greg.android.youcast.FieldDisplayActivity"
tools:showIn="@layout/activity_field_display">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<Spinner
android:id="@+id/run_or_play_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal">
<TextView
android:id="@+id/passer_name_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="@string/passer_name_label"/>
<EditText
android:id="@+id/passer_name_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:hint="@string/passer_name_edit_text"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal">
<TextView
android:id="@+id/receiver_name_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="@string/receiver_name_label"/>
<EditText
android:id="@+id/receiver_name_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:hint="@string/receiver_name_edit_text"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal">
<TextView
android:id="@+id/yards_gained_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="@string/yards_gained"/>
<Spinner
android:id="@+id/yards_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<Button
android:id="@+id/submit_play_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/submit_play_button"/>
</LinearLayout>
</LinearLayout>
和相关的java代码:
...
runOrPlaySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String choice = (String) parent.getItemAtPosition(position);
if (choice.equals("Run")) {
setRunPlayUIFields();
} else if (choice.equals("Pass")) {
setPassPlayUIFields();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
....
private void setRunPlayUIFields() {
passerNameLabel.setText("Runner Name: ");
receiverNameField.setVisibility(View.INVISIBLE);
receiverNameLabel.setVisibility(View.INVISIBLE);
receiverNameField.refreshDrawableState();
passerNameField.setHint("Runner Name");
this.receiverNameField.invalidate();
this.receiverNameLabel.invalidate();
}
private void setPassPlayUIFields() {
passerNameLabel.setText(R.string.passer_name_label);
receiverNameLabel.setVisibility(View.VISIBLE);
receiverNameField.setVisibility(View.VISIBLE);
passerNameField.setHint("Passer Name");
this.receiverNameField.invalidate();
this.receiverNameLabel.invalidate();
}
答案 0 :(得分:2)
在您的XML布局中,进行设置以便设置
android:id="@+id/receiver_name_text_view"
拥有的财产
android:visibility="visible"
您可以尝试,或者如果您愿意,可以尝试使用Java代码 - 执行以下操作:
receiverNameField.setAlpha(0.0f);
让它不可见
receiverNameField.setAlpha(1.0f);
使其可见