3

I am trying to show text with a button click.

Here is my onClick code:

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);

        }
     });
   }
}

What is wrong? When I test it on my phone I only get a blank page.

4

6 回答 6

7

Unless this was a typo in your post, your problem is you haven't declared the proper Activity method

public void onCrate(Bundle savedInstanceState) {

should be

public void onCreate(Bundle savedInstanceState) {

Also, you should probably make your Views member variables (declare them before onCreate() and initialize them inside of onCreate())

Edit

To show/hide your TextView you can use getVisibility to determine what to do. So it would be something like

public void onClick(View v) {
        tidiTekst.setVisibility((tidiTekst.getVisibility() == View.Visible) 
                                                ? View.GONE : View.VISIBLE);
    }
于 2013-10-11T14:44:37.260 回答
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>

code for 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);

        }
    });



}

}

于 2014-11-07T09:44:18.777 回答
0

Define your button and textView variables outside of the on-create method. Until you post the error you are getting, I'm going to assume that you are getting a null pointer because they are going out of scope once onCreate is finished.

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);

        }
    });
}
于 2013-10-11T14:38:57.820 回答
0

Is it working if you decalre your textView as final ?

final TextView tidiTekst = (TextView) findViewById(R.id.tidiTekst);
于 2013-10-11T14:41:49.767 回答
0

Please check your R.layout.activity_uteliv in a WYSIWYG IDE such as Eclipse or IntelliJ IDEA, edit it in graphic mode, it must be something wrong with the layout(xml).

于 2013-10-11T14:46:30.823 回答
0

In 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>

In 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);

        }
    });
于 2018-07-25T06:48:19.423 回答