2012-12-28 41 views
8

Vấn đề của tôi là tôi muốn lấy văn bản chính xác từ Chỉnh sửa văn bản với phông chữ được đặt trong văn bản Chỉnh sửa cũng như Kích thước văn bản, màu văn bản và kiểu văn bản như đậm, nghiêng và gạch dưới.Cách lấy văn bản chính xác từ Chỉnh sửa văn bản và đặt thành dạng xem văn bản trong android

đến bây giờ tôi sử dụng Spannable như Spannable messageText; này và nhận được văn bản từ EditText như thế này

messageText = editText.getText(); 

và đặt vào TextView

textView.setText(messageText); 

Nhưng trong trường hợp này nó trở lại chỉ là chuỗi đơn giản không 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" /> 

giúp tôi, Cảm ơn

+1

Trong tệp xml đặt thuộc tính cho textview giống như trong văn bản chỉnh sửa của bạn .. – Subburaj

+0

tạo màu, phông chữ, kích thước và kiểu theo chương trình cho editText và khi tìm nạp dữ liệu từ editText đặt tất cả thuộc tính đã tạo thành textView nơi bạn đang đi qua chuỗi editText –

+0

@Subburaj không có nó không thay đổi tất cả các thuộc tính của textview –

Trả lời

4

Nếu bạn đang thiết lập màu nền của chỉnh sửa văn bản như thế này

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

Sau đó,

Làm điều này để có được màu nền.

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

Và sau đó đặt màu này thành textView. dự án mẫu

textView.setBackgroundColor(color); 
+0

có tôi đang thử nick –

+1

tôi đã giải quyết được vấn đề của mình chỉ nhận được màu, kích thước và phông chữ từ bản chỉnh sửa văn bản và đặt vào chế độ xem văn bản. cảm ơn và hạnh phúc mã hóa –

+0

vui mừng để giúp bạn giao phối –

1

Hi Tôi đã chạy cho you..I nghĩ rằng bạn đang mong đợi câu trả lời này ..

Đây là 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> 

đang Hoạt động:

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()); 

      } 
     }); 

Tôi nghĩ rằng đây là những gì bạn đang mong đợi.

Các vấn đề liên quan