【问题标题】:How to get the exact text from Edit Text and set into text view in android如何从编辑文本中获取确切的文本并在 android 中设置为文本视图
【发布时间】:2012-12-13 11:09:39
【问题描述】:

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

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

 messageText = editText.getText();

并设置到文本视图中

 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" />

帮帮我,谢谢

【问题讨论】:

  • 在 xml 文件中将 textview 的属性设置为与您的编辑文本中相同..
  • 以编程方式为editText创建颜色、字体、大小和样式,并在从editText获取数据时将所有创建的属性设置为传递editText字符串的textView
  • @Subburaj 不,它没有改变 textview 的所有属性
  • 你能发布你的xml有这个Edittext和textview吗??
  • @nick 我已经以编程方式为 editText 设置了颜色、字体、大小和样式。但我不知道如何为文本视图设置所有这些值

标签: android textview android-edittext


【解决方案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());

            }
        });

我认为这是您所期望的。

【讨论】:

    【解决方案2】:

    如果你是这样设置编辑文本的背景颜色

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

    那么,

    这样做可以获得背景颜色。

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

    然后给textView设置这个颜色。

    textView.setBackgroundColor(color); 
    

    【讨论】:

    • 我解决了我的问题,只是从编辑文本中获取颜色、大小和字体并设置到文本视图中。谢谢,编码愉快
    猜你喜欢
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    相关资源
    最近更新 更多