1

当我通过 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)

任何帮助!!!!

4

3 回答 3

0
// 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")));

    }

}
于 2013-09-10T06:58:06.227 回答
0

实际上,字符串getResource().getString(R.string.s_hello_txt)没有下划线。在 strings.xml 中添加 html 源代码的最佳方法是使用<![CDATA[html source code]]>. 这是一个例子:

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

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

于 2013-09-07T16:40:58.953 回答
0

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

textview.setText(Html.fromHtml(mrng));
于 2013-09-07T16:07:03.190 回答