我有几个具有android:visibility =“ invisible”属性的视图,文本视图和一个按钮。我的目标是单击位于这些“不可见”窗口小部件上方的按钮,以使这些窗口小部件变得可见。我创建了另一个名为“ VisibilityActivity.java”的Java类,并尝试了以下方法,但是由于某种原因,当我运行该应用程序时,该按钮没有执行任何操作。我不知道自己缺少什么。
这是代码:
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class VisibilityActivity extends Activity { private View mVictim; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_property3); mVictim = findViewById(R.id.horizontalRule1); mVictim = findViewById(R.id.TextView03); mVictim = findViewById(R.id.horizontalRule2); Button submitRating = (Button) findViewById(R.id.submitRatingButton); submitRating.setOnClickListener(mVisibleListener); } OnClickListener mVisibleListener = new OnClickListener() { public void onClick(View v) { mVictim.setVisibility(View.INVISIBLE); } }; }
这是xml:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/custom_background" android:isScrollContainer="true" android:orientation="vertical" android:paddingTop="10dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_gravity="center" android:text="@string/ratingsInfo" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black1" /> <View android:layout_width="fill_parent" android:layout_height="2dp" android:background="@color/black1" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:layout_marginBottom="5dp" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:text="@string/yourRating" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/black1" /> <RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="10dp" /> <Button android:id="@+id/submitRatingButton" android:layout_width="275dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="10dp" android:background="@drawable/custom_button" android:text="@string/submitRating" android:textColor="@color/black1" /> <View android:id="@+id/horizontalRule1" android:layout_width="fill_parent" android:layout_height="2dp" android:background="@color/black1" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:layout_marginBottom="5dp" android:visibility="invisible" /> <TextView android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_gravity="center" android:text="@string/summaryInfo" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black1" android:visibility="invisible" /> <View android:id="@+id/horizontalRule2" android:layout_width="fill_parent" android:layout_height="2dp" android:background="@color/black1" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:layout_marginBottom="5dp" android:visibility="invisible" /> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:text="@string/ourRating" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/black1" android:visibility="invisible" /> <RatingBar android:id="@+id/ratingBar2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:stepSize=".01" android:layout_marginBottom="10dp" android:visibility="invisible" /> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:text="@string/overallRating" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/black1" android:visibility="invisible" /> <RatingBar android:id="@+id/ratingBar3" android:color="@color/black1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:stepSize=".01" android:layout_marginBottom="40dp" android:visibility="invisible" /> <Button android:id="@+id/saveContinueButton3" android:layout_width="275dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="15dp" android:background="@drawable/custom_button" android:text="@string/saveContinue" android:textColor="@color/black1" android:onClick="onSaveAndContinue3Clicked" android:visibility="invisible" /> </LinearLayout> </ScrollView>
谢谢。帮助将不胜感激!
我正在更新 user936414 的答案。
OnClickListener mVisibleListener = new OnClickListener() { public void onClick(View v) { if( mText.getVisibility() == View.INVISIBLE ) mText.setVisibility(View.VISIBLE); else mText.setVisibility(View.INVISIBLE); if( mRule1.getVisibility() == View.INVISIBLE ) mRule1.setVisibility(View.VISIBLE); else mRule1.setVisibility(View.INVISIBLE); if( mRule2.getVisibility() == View.INVISIBLE ) mRule2.setVisibility(View.VISIBLE); else mRule2.setVisibility(View.INVISIBLE); } };
另外,您可能想尝试使用View.GONE。