使用按钮显示/隐藏文本

时间:2013-10-11 14:36:20

标签: android textview android-button

我正在尝试点击button来显示文字。

这是我的onClick代码:

public class Uteliv extends Activity {

    public void onCrate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uteliv);

        TextView tidiTekst = (TextView) findViewById(R.id.tidiTekst);
        tidiTekst.setVisibility(View.GONE);

        Button tidiKnapp= (Button) findViewById(R.id.tidiKnapp);
        tidiKnapp.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            tidiTekst.setVisibility(View.VISIBLE);

        }
     });
   }
}

有什么问题?当我在手机上测试时,我只得到一个空白页。

6 个答案:

答案 0 :(得分:4)

除非这是您帖子中的拼写错误,否则您的问题是您尚未声明正确的Activity方法

public void onCrate(Bundle savedInstanceState) {

应该是

public void onCreate(Bundle savedInstanceState) {

此外,您应该制作View个成员变量(在onCreate()之前声明它们并在onCreate()内初始化它们)

修改

要显示/隐藏您的TextView,您可以使用getVisibility来确定要执行的操作。所以它会像

public void onClick(View v) {
        tidiTekst.setVisibility((tidiTekst.getVisibility() == View.Visible) 
                                                ? View.GONE : View.VISIBLE);
    }

答案 1 :(得分:1)

activity_main.xml中

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical"
    android:padding="10dip" >



    <Button
        android:id="@+id/mybtn"
        style="?buttonBarButtonStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:layout_weight="1"
        android:background="#c4c5c7"
        android:shadowColor="#959597"
        android:shadowDx="0"
        android:shadowDy="1"
        android:shadowRadius="1"
        android:text="Button"
        android:textColor="#ffffff"
        android:textSize="30sp"
        android:textStyle="bold" />

    <TextView
    android:id="@+id/myTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="hidden"
    android:visibility="gone"/>

</LinearLayout>
MainActivity.java的代码

private TextView mytxtvw;
private Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mytxtvw=(TextView)findViewById(R.id.myTextView);
    myButton=(Button)findViewById(R.id.mybtn);

    onBtnClick();
}

public void onBtnClick()
{
    myButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mytxtvw.setVisibility((mytxtvw.getVisibility() == View.VISIBLE) 
                    ? View.GONE : View.VISIBLE);

        }
    });



}

}

答案 2 :(得分:0)

在on-create方法之外定义按钮和textView变量。在你发布你得到的错误之前,我将假设你得到一个空指针,因为一旦onCreate完成它们就会超出范围。

TextView tidiTekst;
Button tidiKnapp;

public void onCrate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_uteliv);


    tidiTekst = (TextView) findViewById(R.id.tidiTekst);
    tidiTekst.setVisibility(View.GONE);

    tidiKnapp= (Button) findViewById(R.id.tidiKnapp);
    tidiKnapp.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            tidiTekst.setVisibility(View.VISIBLE);

        }
    });
}

答案 3 :(得分:0)

如果你将textView声明为final,它是否有效?

final TextView tidiTekst = (TextView) findViewById(R.id.tidiTekst);

答案 4 :(得分:0)

请在WYSIWYG IDE(例如Eclipse或IntelliJ IDEA)中检查 R.layout.activity_uteliv ,在图形模式下编辑它,布局(xml)一定有问题。

答案 5 :(得分:0)

在xml中:

 <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:layout_marginTop="@dimen/_6sdp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="MORE"
                android:textStyle="bold"
                android:textColor="@color/darkgray"
                android:textSize="@dimen/_10sdp"
                android:gravity="left"
                android:id="@+id/moreone"/>
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/darkgray"
            android:layout_marginTop="@dimen/_6sdp"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:layout_marginTop="@dimen/_9sdp">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="16) I will not indulge in any way in any act that is against the spirit of law and society. "
                android:textStyle="bold"
                android:textColor="@color/darkgray"
                android:textSize="@dimen/_10sdp"
                android:gravity="left"
                android:id="@+id/tandcone"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:layout_marginTop="@dimen/_6sdp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="17) I will treat all with dignity irrespective and provide them with high level of service and remedies for their dissatisfaction any time during the course of my business as an Independent Associate."
                android:textStyle="bold"
                android:textColor="@color/darkgray"
                android:textSize="@dimen/_10sdp"
                android:gravity="left"
                android:id="@+id/tandctwo"/>
        </LinearLayout>

在MainActivity中:

  moreone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tandcone.setVisibility((tandcone.getVisibility() == View.VISIBLE)
                    ? View.GONE : View.VISIBLE);
            tandctwo.setVisibility((tandctwo.getVisibility() == View.VISIBLE)
                    ? View.GONE : View.VISIBLE);

        }
    });