public class MainActivity extends Activity{ CheckedTextView checkedTextView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = (ListView) findViewById(R.id.listView1); final String[] textViewResource = new String[]{"选项一","选项二","选项三","选项四","选项五"}; checkedTextView = (CheckedTextView) findViewById(R.layout.checkedtext); listView.setAdapter(new ArrayAdapter(this, R.layout.checkedtext, textViewResource )); //清单中要配置checkmark语句: android:checkMark="?android:attr/listChoiceIndicatorSingle" listView.setItemsCanFocus(false);//获得焦点,点击获得事件 //设置单选、多选或者无法选择 listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//设置多选 //用setOnItemClickListener监听item点击事件,如果继承listactivity的话,直接重写onItemClick方法即可 listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Toast.makeText(MainActivity.this, textViewResource[position], Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; }}
注:
android:checkMark="?android:attr/listChoiceIndicatorSingle"
listView.setItemsCanFocus(
false
);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener()
系统范例:
public class List10 extends ListActivity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_single_choice, GENRES)); final ListView listView = getListView(); listView.setItemsCanFocus(false); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } private static final String[] GENRES = new String[] { "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama", "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller" };}