我只希望我的应用程序在主要活动上显示字符串,当应用程序打开时发生的所有事情就是显示字符串数组。不多不少。似乎无法在任何地方找到此信息!
问问题
114 次
3 回答
1
好吧,如果您想在 ListView 中显示它们,请使用 ArrayAdapter 并将字符串添加到适配器。
顺便说一句,如果数组在您的字符串资源中,请使用 getResources().getStringArray(R.array.id) 从您的代码中获取数组。
于 2013-07-22T20:10:54.820 回答
0
向主要活动添加一个文本视图,然后像这样制作一个文本视图的对象。
TextView textView = (TextView) findViewById(R.id.id_of_text_view);
然后将文本设置为文本视图,如下所示:
textView.setText("blah");
从您存储它的地方获取文本
于 2013-07-22T20:12:48.090 回答
0
您可以使用 ListView 组件。
list_cars.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp" >
</TextView>
public class ListCarsActivity extends ListActivity {
static final String[] CARS = new String[] { "BWM", "Volvo", "Mersedes-Benz",
"Honda", "Toyota", "Nexia", "Wolkswagen", "CHevrolet",
"Ferrari", "Volga", "Porshe", "Bentley", "Bugatti" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// it is not needed
// setContentView(R.layout.list_cars);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_cars,CARS));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
于 2013-07-22T20:21:20.197 回答