首先,我想感谢各位在这里,因为我是一个nOOb,并通过阅读你发布的问题和答案学到了很多东西。我试图将大量文本传递给最终用户,并且能够使用新类和.xml文件执行此操作时,这变得很麻烦。我想通过为一组特定的文本字符串设置一个xml布局来简化应用程序流,只需更改@string / ?????通过按钮onclick和setText,但已经知道我无法在xml文件中更改@string的初始值。问题是真的吗?并且有更有效的方法来做到这一点,即(将android:text设置为var并将java中的var设置为特定字符串)或者我是否需要为每个字符串添加新的xml布局? (如果你问我这很浪费)和一点点见解,此时大约有250个不同的字符串,最少5个段落并且还在增长。
这是我到目前为止的代码。
第一个java的片段
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Monlt extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.monolt);
final MediaPlayer buttonsound = MediaPlayer.create(Monlt.this, (R.raw.buttonclick));
Button button1 = (Button) findViewById(id.button1);
Button button2 = (Button) findViewById(id.button2);
Button button3 = (Button) findViewById(id.button3);
Button button4 = (Button) findViewById(id.button4);
Button button5 = (Button) findViewById(id.button5);
Button button6 = (Button) findViewById(id.button6);
Button button7 = (Button) findViewById(id.button7);
Button button8 = (Button) findViewById(id.button8);
Button button9 = (Button) findViewById(id.button9);
Button button11 = (Button) findViewById(id.button11);
Button button12 = (Button) findViewById(id.button12);
Button button13 = (Button) findViewById(id.button13);
Button button14 = (Button) findViewById(id.button14);
Button button15 = (Button) findViewById(id.button15);
Button button16 = (Button) findViewById(id.button16);
Button button17 = (Button) findViewById(id.button17);
Button button18 = (Button) findViewById(id.button18);
Button button19 = (Button) findViewById(id.button19);
Button button21 = (Button) findViewById(id.button21);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
buttonsound.start();
final TextView mview = (TextView) findViewById(R.id.solayout2);
mview.setText("mono1"); //this was my first string to pass
startActivity(new Intent("com.nvar.Sorders.Mono.ASO1"));
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
buttonsound.start();
startActivity(new Intent("com.nvar.Sorders.ASO1"));
final TextView mview = (TextView) findViewById(R.id.solayout2);
mview.setText("@string/mono2");/this is the second string to pass
}
});
`
现在,当我删除onclick中的2个文本视图行时,此代码可以正常工作 所以它调用另一个类Aso1,我想保留以备以后使用。 Aso1 java代码 ` 包com.nvar.Sorders.Mono;
import com.nvar.Sorders.R;
import android.app.Activity;
import android.os.Bundle;
public class Aso1 extends Activity {
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.solayout2);
}
}
`
然后是第一个xml
`
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/solayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mview"/>
<!-- android:text="@string/monoaso1"/>
-->
<!-- this is were i was playing with the strings />
-->
</LinearLayout>
</ScrollView>
` 非常感谢任何帮助,并记住java / android的“noob”!所以,如果有一个我能够或应该看到的样本,请不要犹豫我的头部,并指出我正确的方向。我不介意阅读:) 再次感谢。
答案 0 :(得分:0)
* xml中的@ string / string_name *不是为了更改值而设计的。他们随时为您提供本地化服务。目前,xml位于此处http://developer.android.com/guide/topics/resources/localization.html
res/values/string.xml
你可以使用不同语言的另一个字符串xml,例如france
res/values-fr/strings.xml
您可以通过here.
了解更多相关信息现在,让我们继续讨论如何从java
引用string_nameResources res = Monlt.this.getResources();
mview.setText(res.getString(R.string.string_name));
//do something
mview.setText(res.getString(R.string.mono2));
答案 1 :(得分:0)
显示String
时,您可以使用的是TextView
并且有多行,然后只需在代码中更新它的文本值。它甚至不需要默认文本
<ScrollView
...some params...>
<TextView
android:id="@+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="10"/>
</ScrollView>
这会给你一个TextView
10行长。然后在代码中你可以做这样的事情来更新值:
String longText = getVeryLongText();
((TextView)findViewById(R.id.my_text)).setText(longText);
然后你会看到一些看似可滚动的文字段落