xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="你来自于那个省?"
android:textSize="25sp"
/>
<Spinner
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/spinner_pro"
android:layout_marginLeft="30dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="你来自于哪个城市?"
android:textSize="25sp"
/>
<Spinner
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/spinner_city"
android:layout_marginLeft="30dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="你来自于哪个班级?"
android:textSize="25sp"
/>
<AutoCompleteTextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textSize="22sp"
android:layout_margin="30dp"
android:id="@+id/autotext"
/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/a"
android:text="a"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b"
android:text="b"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/c"
android:text="c"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/d"
android:text="d"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/e"
android:text="e"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="按键"/>
</LinearLayout>
<ListView
android:layout_width="400dp"
android:layout_height="match_parent"
android:id="@+id/mylist"
android:layout_margin="30dp"
/>
<ListView
android:layout_width="400dp"
android:layout_height="match_parent"
android:id="@+id/myADlist"
android:layout_margin="30dp"
/>
<GridView
android:layout_width="900dp"
android:layout_height="match_parent"
android:id="@+id/myADgrid"
android:layout_margin="30dp"
android:numColumns="3"
/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Java文件
public class threeActivity extends AppCompatActivity {
Spinner Spinner_Pro, Spinner_City;
AutoCompleteTextView AutoText;
ListView LV, LV_AD;
GridView GV_AD;
String[] String_Pro = {"广东省", "广西省", "福建省", "海南省", "浙江省", "江苏省",
"湖南省", "湖北省", "安徽省", "江西省", "山东省", "河南省",
"山西省", "河北省", "黑龙江", "辽宁省", "吉林省", "内蒙古",
"新疆", "西藏", "宁夏", "青海省", "台湾省", "甘肃省",
"北京", "天津", "重庆", "四川省", "云南省", "上海"};
ArrayList arrayList = new ArrayList();
String[] StudentName = {"黄琼妮", "刘君豪", "尹冠朗", "梁雁廷",
"赖泽华", "黄瑾勋", "李兆涯", "邓维晟", "吴忠飞", "陈首源",
"卢培纯", "梁海亮", "吴金柱", "孙杰", "蓝子健", "刘宇康",
"王春豪", "谢桩", "刘艳君", "林康杰", "郭之泳", "丁争彦",
"周烙谦", "陈佳伟", "叶景宙", "叶苏贵", "唐瑾", "吴孟联",
};
String[] StudentNum = {"17115062029", "18125041001", "18125041002", "18125041003",
"18125041009", "18125041010", "18125041011", "18125041012", "18125041014",
"18125041017", "18125041019", "18125041024", "18125041029", "18125041032",
"18125041033", "18125041034", "18125041035", "18125041044", "18125041045",
"18125041046", "18125041047", "18125041050", "18125041051", "18125041053",
"18125041054", "18125041055", "18125041057", "18125041058",
};
int[] HeadPic = {R.mipmap.ab, R.mipmap.ch, R.mipmap.cl,
R.mipmap.cwt, R.mipmap.cxk, R.mipmap.dc, R.mipmap.dlrb,
R.mipmap.glnz, R.mipmap.he, R.mipmap.hjh, R.mipmap.hzt, R.mipmap.jd,
R.mipmap.lb, R.mipmap.lcr, R.mipmap.lcy, R.mipmap.ldh,
R.mipmap.lh, R.mipmap.lmg, R.mipmap.lmyz, R.mipmap.lq,
R.mipmap.lyf, R.mipmap.qzy, R.mipmap.tly, R.mipmap.wy,
R.mipmap.wyb, R.mipmap.ym, R.mipmap.zbc, R.mipmap.zjl,
R.mipmap.zly, R.mipmap.zry, R.mipmap.zyl, R.mipmap.zyx
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.three_layout2);
setTitle("123");
Button();
Spinner_Pro = findViewById(R.id.spinner_pro);
Spinner_City = findViewById(R.id.spinner_city);
AutoText = findViewById(R.id.autotext);
LV = findViewById(R.id.mylist);
LV_AD = findViewById(R.id.myADlist);
GV_AD = findViewById(R.id.myADgrid);
final ArrayList arrayListpro = new ArrayList<String>();
for (int i = 0; i < String_Pro.length; i++) {
arrayList.add(String_Pro[i]);
}
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(
threeActivity.this,
android.R.layout.simple_spinner_item,
arrayList);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner_Pro.setAdapter(arrayAdapter);
Spinner_Pro.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(threeActivity.this, "我来自于" + String_Pro[position], Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
ArrayAdapter<CharSequence> Adapter_City = ArrayAdapter.createFromResource(
threeActivity.this, R.array.city, android.R.layout.simple_spinner_item);
Adapter_City.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner_City.setAdapter(Adapter_City);
Spinner_City.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String[] cityname = getResources().getStringArray(R.array.city);
Toast.makeText(threeActivity.this, "我的城市是" + cityname[position].toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
ArrayAdapter<CharSequence> Adapter_glass = ArrayAdapter.createFromResource(
threeActivity.this,
R.array.glass,
android.R.layout.simple_list_item_1
);
AutoText.setAdapter(Adapter_glass);
ArrayAdapter listAdapter = new ArrayAdapter(
threeActivity.this,
android.R.layout.simple_list_item_1,
arrayListpro);
LV.setAdapter(listAdapter);
LV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(threeActivity.this, "我点击的是" + String_Pro[position].toString(), Toast.LENGTH_SHORT).show();
}
});
ArrayList<HashMap<String, Object>> users = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < StudentName.length; i++) {
HashMap<String, Object> user = new HashMap<String, Object>();
user.put("img", HeadPic[i]);
user.put("name", StudentName[i]);
user.put("num", StudentNum[i]);
user.put("hometown", String_Pro[i]);
users.add(user);
}
SimpleAdapter StudentAdapter = new SimpleAdapter(
threeActivity.this,
users,
R.layout.item_layout,
new String[]{"img", "name", "num", "hometown"},
new int[]{R.id.pic_head, R.id.tx_name, R.id.tx_num, R.id.tx_home}
);
LV_AD.setAdapter(StudentAdapter);
LV_AD.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(threeActivity.this, StudentName[position].toString(), Toast.LENGTH_SHORT).show();
}
});
GV_AD.setAdapter(StudentAdapter);
}
}
新建一个XML文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="city">
<item>韶关</item>
<item>广州</item>
<item>深圳</item>
<item>珠海</item>
<item>中山</item>
<item>茂名</item>
<item>云浮</item>
<item>潮州</item>
</string-array>
<string-array name="glass">
<item>18物联网1班</item>
<item>18物联网2班</item>
<item>18物联网3班</item>
<item>18物联网4班</item>
<item>18通信工程1班</item>
<item>18通信工程2班</item>
<item>18通信工程3班</item>
<item>17通信工程1班</item>
<item>17通信工程2班</item>
<item>17通信工程3班</item>
</string-array>
</resources>