Android - 如何将字符串从Activity传递到布局中?

4

我该如何将一个字符串从一个活动传递到布局中?我可以使用以下活动代码将数组从活动传递到布局

    ListView remedyList = (ListView) findViewById(R.id.ListView_Symptom);
    String remedyArray[] = new String[30];
    ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, remedyArray);
    remedyList.setAdapter(adapt);

有没有一种更简单的方法,只传递一个字符串而不是任何字符串数组,从一个活动传递到布局中?

1个回答

5
如果您想向布局传递值,希望这些值在布局的元素(例如TextView)中显示。假设您在布局中有一个TextView。
<?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"
    android:background="#FFFFFF"
    >
<TextView  
    android:id="@+id/myTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"     
    />
</LinearLayout>

然后你可以将一个字符串值设置到你的TextView中。
String myText = "James";
TextView myTextView= (TextView) findViewById(R.id.myTextView);
myTextView.setText(myText);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接