当通过put extra发送字符串时,某些单词下的下划线已经消失

时间:2013-09-07 15:54:20

标签: android textview underline

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

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)带下划线

任何帮助!!!!

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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)显示带下划线的字符串

答案 2 :(得分: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")));

    }

}