【问题标题】:Hide text only but not whole textview仅隐藏文本但不隐藏整个文本视图
【发布时间】:2013-09-19 14:36:11
【问题描述】:

我的目标是隐藏文本并保持 textview 在活动中占据其空间,我已经通过使用将我的文本设置为不可见:

tv.setVisibility(View.INVISIBLE);

当按钮单击它显示文本时,一切正常,除了我的代码导致隐藏整个 textview 不仅文本,因为我将我的 textview 背景设置为可绘制形状,在文本周围形成红色边框:

android:background="@drawable/border1" 

  <TextView 
  android:id="@+id/introclusion_tv3" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:background="@drawable/border1" 
  android:textSize="20sp" /> 

当启动应用程序时,您只能看到空白空间,在单击按钮后将用文本填充,但那里没有边框(来自形状背景),所以它隐藏了整个 textview,我需要它来隐藏文本并保留当 text 设置为 INVISIBLE 时显示其背景的 textview,

任何帮助将不胜感激,谢谢。

我就是这样做的:

  TextView tv11=(TextView)findViewById(R.id.introclusion_tv3);
    tv11.setText(Html.fromHtml(getString(R.string.introclusion_one)));
    tv11.setVisibility(View.INVISIBLE); 

然后点击但是输入正确的密码后,它会显示文本为:

   Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);

        dialogButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 dialog.dismiss();

                EditText password = (EditText) dialog.findViewById(R.id.password);

                if( password.getText().toString().length() > 0 ) {
                    if( password.getText().toString().equals("test")) {

                        TextView tv11=(TextView)findViewById(R.id.introclusion_tv3);
                        tv11.setTypeface(FontFactory.getBFantezy(getBaseContext()));
                        tv11.setText(Html.fromHtml(getString(R.string.introclusion_one)));
                        tv11.setVisibility(View.VISIBLE);
                        }

【问题讨论】:

    标签: android textview


    【解决方案1】:

    透明文字颜色隐藏文字:

    <TextView 
      android:id="@+id/introclusion_tv3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/border1" 
      android:textColor="@android:color/transparent"
      android:textSize="20sp" /> 
    

    当你想显示你的文本时,使用方法setTextColor()以编程方式更改文本颜色:

    tv11.setTextColor(color);
    

    【讨论】:

    • 这是天才。
    【解决方案2】:
    // try this way
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/border1">
    
            <TextView
                android:id="@+id/introclusion_tv3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp" />
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      你可以做一个简单的技巧:写两个字符串

      让我们在你的第一段和第二段代码中删除这一行

       tv11.setVisibility(View.INVISIBLE); 
      

       tv11.setVisibility(View.VISIBLE);
      

      原来如此

      TextView tv11=(TextView)findViewById(R.id.introclusion_tv3);
      tv11.setText(Html.fromHtml(getString(R.string.introclusion_one)));
      

      然后在第二部分写如下;

        if( password.getText().toString().equals("test")) {
      
             TextView tv11=(TextView)findViewById(R.id.introclusion_tv3);
             tv11.setTypeface(FontFactory.getBFantezy(getBaseContext()));                         
             tv11.setText(Html.fromHtml(getString(R.string.introclusion_one_appear)));                       
                          }
      

      第一个字符串为空

       <string name="introclusion_one">
      

      第二个字符串,您将在其中写入文本

       <string name="introclusion_one_appear">
      

      希望能帮到你。

      【讨论】:

        【解决方案4】:

        将文本视图中的内容保存为这样的字符串:

                String x = (String)tv11.getText();
        

        然后像这样使文本视图为空:

        String x = "";
        for(int i = 0; i < x.length(); i++){
        x +=" ";
        }
        tv11.setText(x);
        

        让 textview 再次可见:

        tv11.setText(x);
        

        【讨论】:

          猜你喜欢
          • 2013-09-01
          • 2016-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多