UI代码如下 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView> </LinearLayout>
设置ListView的 Item样式 xml文件如下:
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip"
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBox android:id="@+id/CheckBox01"
android:layout_alignParentRight="true"
android:paddingTop="12dip"
android:layout_width="wrap_content"
android:focusable="false"
android:layout_height="wrap_content"></CheckBox>
<TextView android:text="TextView1" android:id="@+id/topTextView"
android:layout_width="fill_parent"
android:textSize="20dip"
android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView2" android:id="@+id/bottomTextView"
android:layout_below="@+id/topTextView" android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
</RelativeLayout>
到这一步,顺便解答上面第4个问题:如何设置在 ListView中调协CheckBox之后还可以执行单击或长按事件,在Android中 CheckBox的点击事件优先级比ListView高,当页面一加载时,会默认聚焦在CheckBox上,为了防止这个问题出来,我们要在Android的控件上的属性加上Bool值: android:focusable="false"最后就是Java源代码,之后我再一一解答上面的所有问题:
package cn.terry;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
public class testListViewAcitivity extends Activity {
private ListView myListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListView=(ListView)findViewById(R.id.ListView01);
ArrayList<HashMap<String, Object>> listitem=new ArrayList<HashMap<String,Object>>();
for(int i=0;i<10;i++)
{
HashMap<String, Object> map=new HashMap<String, Object>();
map.put("ItemTitle", "文章"+i);
map.put("ItemText", "Android 小項目"+i);
listitem.add(map);
}
SimpleAdapter listitemAdapter=new SimpleAdapter(this,//上下文
listitem, //數據
R.layout.listview_style,//listItem的xml實現
new String[]{"ItemTitle","ItemText"}, //從map的批定Key
new int[]{R.id.topTextView,R.id.bottomTextView} //到要實現的控件上
);
myListView.setAdapter(listitemAdapter);
myListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
RelativeLayout lr=(RelativeLayout)arg1;
TextView mText=(TextView)lr.getChildAt(1);
Toast.makeText(testListViewAcitivity.this, "你點擊了第"+arg2+"項的"+mText.getText().toString(),
1000).show();
}
});
myListView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
ListView lr=(ListView)v;
RelativeLayout myte=(RelativeLayout)lr.getChildAt(0);
TextView dd=(TextView)myte.getChildAt(1);
menu.setHeaderIcon(R.drawable.icon);
menu.setHeaderTitle(dd.getText().toString());
menu.add(1, 0, 0, "高亮");
menu.add(0, 1, 0, "置頂");
}
});
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
String Temp="";
switch (item.getItemId()) {
case 0:
Temp="高亮";
break;
case 1:
Temp="置頂";
break;
default:
break;
}
Toast.makeText(this, Temp+"處理", 1000).show();
return super.onContextItemSelected(item);
}
}
ArrayList<HashMap<String, Object>> listitem=new ArrayList<HashMap<String,Object>>();
for(int i=0;i<10;i++)
{
HashMap<String, Object> map=new HashMap<String, Object>();
map.put("ItemTitle", "文章"+i);
map.put("ItemText", "Android 小項目"+i);
listitem.add(map);
}
SimpleAdapter listitemAdapter=new SimpleAdapter(this,//上下文
listitem, //數據
R.layout.listview_style,//listItem的xml實現
new String[]{"ItemTitle","ItemText"}, //從map的批定Key
new int[]{R.id.topTextView,R.id.bottomTextView} //到要實現的控件上
);
myListView.setAdapter(listitemAdapter);
myListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
RelativeLayout lr=(RelativeLayout)arg1;
TextView mText=(TextView)lr.getChildAt(1);
Toast.makeText(testListViewAcitivity.this, "你點擊了第"+arg2+"項的"+mText.getText().toString(),
1000).show();
}
});
void cn.terry.testListViewAcitivity.onCreate(...).新建 OnCreateContextMenuListener() {...}.onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
指定: OnCreateContextMenuListener 中的 onCreateContextMenu(...)
public abstract void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
Since: API Level 1
Called when the context menu for this view is being built. It is not safe to hold onto the menu after this method returns.
Parameters
menu The context menu that is being built
v The view for which the context menu is being built
menuInfo Extra information about the item for which the context menu should be shown. This information will vary depending on the class of v.
menu.setHeaderIcon(R.drawable.icon);
menu.setHeaderTitle(dd.getText().toString());
menu.add(1, 0, 0, "高亮");
menu.add(0, 1, 0, "置頂");
boolean cn.terry.testListViewAcitivity.onContextItemSelected(MenuItem item)
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
String Temp="";
switch (item.getItemId()) {
case 0:
Temp="高亮";
break;
case 1:
Temp="置頂";
break;
default:
break;
}
Toast.makeText(this, Temp+"處理", 1000).show();
return super.onContextItemSelected(item);
}