[英]retrieve EditText multiline text as it is android?
我想從編輯文本中獲取文本並在TextView中顯示。但是每當我使用getText()獲取文本時,它都會在一行中顯示文本。 如何按原樣在多行EditText中輸入文本。 請給我指導?
拉金達
我不明白您為什么遇到這個問題。 當您從EditText中獲取一段文本時,它應保持“格式”,例如換行符。
例如,嘗試下面的代碼(我自己進行了測試)!
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/m_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
main.java
public class MainTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.m_button);
button.setOnClickListener(btnListener);
}
//button listener
private OnClickListener btnListener = new OnClickListener() {
public void onClick (View view) {
EditText et = (EditText) findViewById(R.id.et);
String str = et.getText().toString();
Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(str);
}
};
}
Toast和TextView都保留換行符。 您可以自己嘗試。
也許您發布您的代碼(至少相關位),我們可以為您提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.