首页 文章

如何从编辑文本中获取确切的文本并在android中设置为文本视图

提问于
浏览
8

我的问题是我希望从编辑文本中获取精确文本,其中包含在编辑文本中设置的字体以及文本大小,文本颜色和文本样式,如粗体,斜体和下划线 .

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

messageText = editText.getText();

并设置为textview

textView.setText(messageText);

但在这种情况下,它只返回简单的字符串而不是 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" />

帮帮我,谢谢

2 回答

  • 4

    如果您正在设置编辑文本的背景颜色,请执行此操作

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

    然后,

    Do this to get the background color.

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

    And then set this color to the textView.

    textView.setBackgroundColor(color);
    
  • 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());
    
                }
            });
    

    我想这就是你所期待的 .

相关问题