简体   繁体   English

我想使用android中的按钮隐藏和取消隐藏文本; 该代码是正确的,但仍然无法正常工作

[英]I want to hide and unhide text using a button in android ; The code is correct but it still doesn't work

Here is the xml file 这是xml文件

<?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 这是java文件

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. 我是android开发的新手,因此请忽略此怀疑是否愚蠢。 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? 我通过使用Log进行了手动检查,发现getText可以正常工作,只要单击按钮(此处为b1),都不会发生任何事情,如何解决此问题?

Why don't you simply check the TextView visibility and perform your actions based on that ? 为什么不简单地检查TextView可见性并基于此执行操作? 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. 使用equals比较字符串。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我只想通过android发送get请求,但是它不起作用,我的代码如下, - I want to simply send get request through android but it doesn't work my code is as follow, 我想在单击按钮1时打印数字1,但它不起作用 - I want to print number 1 when click the button 1 but it doesn't work 为什么这段代码不能改变Android上的文字? - Why doesn't this code to change text on Android work? 一旦失去焦点,我想从文本字段中获取文本。 为此,我尝试使用“ this”运算符,但它不起作用 - I want to get the text from the textfields as soon as their focus is lost. for this i tried using the “this” operator but it doesn't work Java android动画隐藏和取消隐藏视图和调整大小按钮 - Java android animate hide and unhide view and resize button 我似乎在代码中找不到任何逻辑错误,但仍无法按预期工作 - I can't seem to find any logic errors in the code, but it still doesn't work as expected 当我单击按钮时,它不起作用(Android) - When I click the Button it doesn't work (Android) Android:按钮不起作用,但代码未显示任何错误 - Android: Button doesn't work, but code doesn't show any error 我刚刚编写了我的第一个android应用程序,但3个小时后仍然无法正常工作 - I have just written my first android app, and after 3 hours it still doesn't work 我想编写一个代码来使用Java验证不同类型的日期,但带有T和Z的日期仍显示无效 - I want to write a code to validate different types of dates using java but dates with T and Z are still showing Invalid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM