我试图在TextView
内显示两个字符串变量apple和banana。
我试图添加换行符并且不起作用。我唯一没有尝试过的是将变量保存到文件中并将文件读入TextView。
这是我的java代码:`
public class MainActivity extends ActionBarActivity {
private Button button;
TextView tv;
String apple = "bananas" + "\n";
String banana = "apples" + "\n";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv1);
activatebutton();
}
private void activatebutton() {
// TODO Auto-generated method stub
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// code here
tv.setText(apple);
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setText(banana, null);
tv.setMovementMethod(new ScrollingMovementMethod());
}
});
}
}
以下是Xml
代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="73dp"
android:layout_marginTop="44dp"
android:text="@string/button" />
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="90dp"
android:background="#FFFF00"
android:gravity="top"
android:lines="10"
android:maxLines="10"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical"
android:textSize="18sp" />
</RelativeLayout>
我做错了什么?
答案 0 :(得分:1)
只需替换:
tv.setText(apple);
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setText(banana, null);
tv.setMovementMethod(new ScrollingMovementMethod());
由:
tv.setText(apple + banana);
tv.setMovementMethod(new ScrollingMovementMethod());
答案 1 :(得分:0)
试试这个:
private void activatebutton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//First option
//By this
tv.setText(apple + banana);
tv.setMovementMethod(new ScrollingMovementMethod());
}
});
}
或者,如果你想动态。
private void activatebutton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Second option
//Or dynamically
String oldText = tv.getText().toString();
if(oldText.length > 0){
tv.setText(oldText + "\n" + "your string here");
} else {
tv.setText("your string here");
}
tv.setMovementMethod(new ScrollingMovementMethod());
}
});
}
答案 2 :(得分:0)
在XML文件中编辑EditText属性,如下所示
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/two"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textMultiLine" />
在您的MainActivity.java文件中
EditText editText;
String apple = "bananas" ;
String banana = "apples" ;
editText=(EditText) findViewById(R.id.editText1);
editText.setText(apple+"\n"+banana);
对我来说效果很好。我希望,它会对你有所帮助。