Here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_hide"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.andrew.hide.Hide">
<TextView
android:text="This is the text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="81dp"
android:id="@+id/textView"
android:textSize="40sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Hide"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/button"
android:textSize="28sp"
android:onClick="hide"
android:layout_marginBottom="184dp" />
</RelativeLayout>
Here is the java file
package com.example.andrew.hide;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Hide extends Activity {
Button b1;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hide);
b1=(Button) findViewById(R.id.button);
t1=(TextView) findViewById(R.id.textView);
String text=b1.getText().toString();
}
public void hide(View view) {
if (b1.getText().toString() == "Hide") {
t1.setVisibility(View.INVISIBLE);
b1.setText("Unhide");
}
if (b1.getText().toString() == "Unhide") {
t1.setVisibility(View.VISIBLE);
b1.setText("Hide");
}
}
}
I am new to android development , so ignore if this doubt is silly. I manually checked by using Log and I found that getText works perfectly Nothing happens whenever i click the button (b1 here),How do I fix this?
Why don't you simply check the TextView
visibility and perform your actions based on that ? It's all the setters and getters right ?
b1.onClickListener(new OnClickListener() {
if (t1.getVisibility() == View.Visible) {
t1.setVisibilty(View.GONE);
//--- your code
} else {
t1.setVisibilty(View.VISIBLE);
//--- your code
}
);
Use equals
to compare strings.
Try this,
public void hide(View view) {
String text = b1.getText().toString();
if (text.equals("Hide")) {
t1.setVisibility(View.INVISIBLE);
b1.setText("Unhide");
} else if (text.equals("Unhide")) {
t1.setVisibility(View.VISIBLE);
b1.setText("Hide");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.