1)效果图如下
2)关键代码如下
package com.example.androidtest; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import android.view.*; public class MainActivity extends Activity { private ListView listView1=null; private ArrayAdapter<String> adapter=null; private String[] cityStrings=new String[]{ "北京","上海","广州","深圳","苏州", "杭州","重庆","成都","南京","武汉", "扬州","无锡","常州","南通","嘉兴", "绍兴","宁波","湖州","舟山","金华", "丽水","雅安","乐山","绵羊","德阳"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView1=(ListView)findViewById(R.id.listView1); adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, cityStrings); listView1.setAdapter(adapter); listView1.setOnItemClickListener(new ListView.OnItemClickListener(){ public void onItemClick(AdapterView<?> parent,View view,int position,long arg3) { String msgString=parent.getItemAtPosition(position).toString(); showToast(msgString); } }); } private void showToast(CharSequence msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }