Make Textview Visible by Pressing a Button

你说的曾经没有我的故事 提交于 2020-01-02 19:28:14

问题


I have my first class assignment in android app programming, hopefully you guys could help with this.

I need to connect a code to a button so when I press it my textview will appear.

In my XML-file I have

   <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="22dp"
    android:text="@string/ok_knappen" 
    android:textColor="@color/button_dark_text"
    android:onClick="b"
    />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_alignLeft="@+id/textView2"
    android:layout_marginBottom="15dp"
    android:text="@string/Beskrivning"
    android:visibility="invisible" />

In my Activity I have

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextView t=(TextView)findViewById(R.id.textView3);
    Button b= (Button) findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        t.setVisibility(View.VISIBLE);
        }
    });
}

No errors occurred. My text is invisbile but when I click the button nothing happens. What is wrong?


回答1:


In TextView add:

android:visibility="invisible"

In Java code:

public void b(View view) {

    EditText numerField = (EditText) findViewById(R.id.button1);
    TextView  tex = (TextView) findViewById(R.id.textView2);
    tex.setVisibility(View.VISIBLE);
    //do sth
}



回答2:


just change your textView given by

android:visibility="gone"



回答3:


Try removing the onClick attribute from the Button.

The onClick attribute should be followed by a method. This method "b" does not exist, but since you have set the onClickListener in your code it does not seem necessary here I think.




回答4:


Remove the onClick entry in the XML, or create a method with the signature "public void b(View v)" in your activity. The assignment of the onClick in the XML tries to find a method with that kind of listener. It is possible that the XML is overriding the Java assigning of the onClick listener.



来源:https://stackoverflow.com/questions/20203208/make-textview-visible-by-pressing-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!