几天前我会搜索这个,但不幸的是我找不到我的问题的答案。我目前正在开发一个 android 项目,当您单击一个按钮时,它会向您显示一个对话框,其中包含来自字符串数组资源的项目的列表视图。谢谢。
这是我的字符串数组:
<string-array name="heart_attacks_and_shock">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
这是我的代码:
import java.util.ArrayList;
import com.sap.BSMA.R.string;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
@SuppressLint("ParserError")
public class FirstaidActivity extends Activity {
Resources res = getResources();
String[] has = res.getStringArray(R.array.heart_attacks_and_shock);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstaid_layout);
ImageButton has_button = (ImageButton) findViewById(R.id.HAS);
has_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//set up dialog
Dialog dialog = new Dialog(FirstaidActivity.this);
dialog.setContentView(R.layout.firstaidcategoryoutput_layout);
dialog.setTitle("Heart attack and shock");
dialog.setCancelable(true);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,has);
setListAdapter(adapter);
dialog.show();
}
});
}
}
我用于弹出窗口的 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/mainbackground">
<ListView
android:id="@+id/listExample"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CCCCCC"
android:choiceMode="singleChoice"
/>
</RelativeLayout>