【问题标题】:when sending strings via put extra the underline under some words has gone当通过 put extra 发送字符串时,某些单词下的下划线已经消失
【发布时间】:2013-09-11 13:52:30
【问题描述】:

当我通过 put extra 发送字符串时,带下划线的单词不会加下划线

<string name="s_hello_txt">\n{ <u>hello all</u> }\n</string>

MainActivity 按钮代码

public void c_hello(View v){
    Intent hello= new Intent(MainActivity.this,
            MainTextActivity.class);
    intent_collection_e3tiraf.putExtra("key",getResources().getString(R.string.s_hello_txt));
    startActivity(hello);
    finish();
}

MainActivityText onCreate 代码

textview = (TextView) findViewById(R.id.id_text_txt);
    Intent n = getIntent();
    String mrng = n.getStringExtra("key");
    textview.setText(mrng);

如果我用直接字符串输入文本,它将带下划线

例如,如果我放入 MainActivityText(activity_maintext.xml) 的布局

<TextView
    android:id="@+id/id_dailyprayers_txt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/s_hello_txt"
    android:textSize="30sp" />

MainActivityText 中的 textview 显示带下划线的文本(hello all)

任何帮助!!!!

【问题讨论】:

    标签: android textview underline


    【解决方案1】:

    只要字符串仍然有下划线 html,您应该能够利用 Html.fromHtml 方法来设置字符串的样式。

    textview.setText(Html.fromHtml(mrng));
    

    【讨论】:

    【解决方案2】:

    其实getResource().getString(R.string.s_hello_txt)这个字符串是没有下划线的。 在strings.xml 中添加html 源代码的最好方法是使用&lt;![CDATA[html source code]]&gt;。这是一个例子:

    <string name="s_hello_txt"><![CDATA[<u>Text</u>]]></string> 
    

    然后用Html.fromHtml(mrng)显示下划线字符串

    【讨论】:

    • 伙计,它工作得很好,谢谢,但是我的字符串中的这个 html 现在还有另一个问题,我使用 \n 转到换行符,但是当我使用 html 时没有新行
    • &lt;![CDATA[&lt;br/&gt;&lt;u&gt;Text&lt;/u&gt;&lt;br/&gt;]]&gt;
    • 如果我有这个字符串文件,它不起作用如何更改它以使用 html \nhello all\n\n 你今天好吗\n\neverthing没问题\n
    【解决方案3】:
    // Try This One This Will Help For Your Acheivement
    **String.xml**
     <string name="s_hello_txt">&lt;br/>{ &lt;u>hello all&lt;/u> }&lt;br/></string>
    
    **activity_main1.xml**
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
       <TextView 
           android:id="@+id/txtValue"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/s_hello_txt"/>
    
       <Button 
           android:id="@+id/btnClick"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="GoTo Second Activity"/>
    
    </LinearLayout>
    
    **MainActivity1 Activity**
    public class MainActivity1 extends Activity {
    
        private Button btnClick;
        private TextView txtValue;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main1);
    
            txtValue = (TextView)findViewById(R.id.txtValue);
            btnClick = (Button)findViewById(R.id.btnClick);
    
            txtValue.setText(Html.fromHtml(getString(R.string.s_hello_txt)));
            btnClick.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(MainActivity1.this, MainActivity2.class);
                    intent.putExtra("EXTRA",getString(R.string.s_hello_txt));
                    startActivity(intent);
                }
            });
        }
    
    }
    
    **activity_main2.xml**
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/txtValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    
    **MainActivity2 Activity**
    public class MainActivity2 extends Activity {
    
        private TextView txtValue;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
            txtValue = (TextView)findViewById(R.id.txtValue);
    
            txtValue.setText(Html.fromHtml(getIntent().getStringExtra("EXTRA")));
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-20
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 2017-01-27
      • 1970-01-01
      • 2018-09-19
      • 2013-05-22
      相关资源
      最近更新 更多