android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<ListView android:id=“@+id/listView”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
//MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simpleadapter);
listView=(ListView)findViewById(R.id.listView);
//1.准备好数据源,循环为listView添加数据
for(int i=0;i<6;i++){
Map<String,Object> items=new HashMap<String,Object>();
items.put(“pic”,images[i]);
items.put(“name”,name[i]);
list_map.add(items);
}
//2、创建适配器
SimpleAdapter simpleAdapter=new SimpleAdapter(
MainActivity.this,
list_map,
R.layout.list_items,
new String[]{“pic”,“name”},
new int[]{R.id.items_imageView1,R.id.items_textView1}
); l
istView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position, long id) {
/ / Toast.makeText(MainActivity.this, name[position],
Toast.LENGTH_LONG).show();
Toast toast=Toast.makeText(MainActivity.this, name[position], Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER|Gravity.CENTER,0,0);
toast.show();
}
});
实现效果:
AlertDialog可以在当前的界面弹出一个对话框,这个对话框是置顶于所有界面元素上的,能够屏蔽掉其他控件的交互能力,因此,AlertDialog一般都是用于提示一些非常重要的内容或者警告信息。比如为了防止用户误删重要内容,在删除前弹出一个确认对话框。
代码----
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout tableLayout = (TableLayout) getLayoutInflater().inflate(R.layout.activity_main,null);
new AlertDialog.Builder(this).setView(tableLayout).create().show();
}
实现效果:
ActionMode。ActionMode是Android 3.0以后出现的,我们可以使用AppCompat库使ActionMode兼容至Android 2.1。Android 3.0以前,我们处理列表的长按事件经常使用Context Menu,Android3.0以后,我们有了新的选择:ActionMode
代码----基于ActionMode的上下文菜单
private String[] data = {“One”, “Two”, “Three”, “Four”, “Five”, “Six”, “Seven”, “Eight”, “Nine”,“Ten”};
private SelectionAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new SelectionAdapter(this, R.layout.layout_row, R.id.textView1, data);
setListAdapter(mAdapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
private int nr = 0;
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub mAdapter.clearSelection(); }
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
enuInflater inflater = getMenuInflater();