8

我的问题是我想从编辑文本中获取确切的文本,其字体设置在编辑文本中,以及文本大小、文本颜色和文本样式,如粗体、斜体和下划线。

直到现在我像这样使用 Spannable Spannable messageText;并像这样从 EditText 获取文本

 messageText = editText.getText();

并设置为 textview

 textView.setText(messageText);

但在这种情况下,它只返回简单的字符串 not color,font,size and style

EditText

   <EditText
        android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="180dp"
        android:inputType="textMultiLine"
        android:singleLine="false"
        android:tag="no"
        android:textColor="#000000"
        android:textSize="15sp"
        android:textStyle="normal"
        android:typeface="normal" />

TextView

<TextView
            android:id="@+id/preview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text=""
            android:textColor="#000000"
            android:textSize="15sp"
            android:textStyle="normal"
            android:typeface="normal" />

帮帮我,谢谢

4

2 回答 2

4

如果您要像这样设置编辑文本的背景颜色

 editText.setBackgroundDrawable(new PaintDrawable(Color.YELLOW));

然后,

这样做以获得背景颜色。

PaintDrawable drawable = (PaintDrawable) editText.getBackground();
int color = drawable.getPaint().getColor();

然后将此颜色设置为 textView。

textView.setBackgroundColor(color); 
于 2012-12-28T06:22:54.323 回答
1

嗨,我已经为你运行了示例项目..我想你期待这个答案..

这是xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

     <EditText
        android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="180dp"
        android:inputType="textMultiLine"
        android:singleLine="false"
        android:tag="no"
        android:textColor="#1DA237"
        android:textSize="15sp"
        android:textStyle="italic"
        android:typeface="normal" />


    <TextView
            android:id="@+id/preview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="subbu"
            android:textColor="#1DA237"
            android:textSize="15sp"
            android:textStyle="italic"
            android:typeface="normal" 
            android:paddingBottom="50dp"
            android:layout_below="@+id/message"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"

        android:layout_marginRight="29dp"
        android:layout_marginTop="93dp"
        android:text="Button" 
        android:layout_below="@+id/preview"/>

</RelativeLayout>

活动代码:

 final EditText text=(EditText)findViewById(R.id.message);

        Button b=(Button)findViewById(R.id.button1);
        final TextView t=(TextView)findViewById(R.id.preview);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                t.setText(text.getText().toString());

            }
        });

我认为这是你所期待的。

于 2012-12-28T06:31:09.153 回答