当前位置: 编程技术>移动开发
本页文章导读:
▪ListView的长按菜单_源码分析 ListView的长按菜单___源码分析
ListView的长按菜单___源码分析Android的listview可以长按弹出来一个菜单。 今天就跟了下代码大概看了下弹出菜单的流程。 我们实现一个菜单长按步骤通常如下:.........
▪ 多线程施用httpclient 多线程使用httpclient
public class ClientMultiThreadedExecution {
public static void main(String[] args) throws Exception {
// Create and initialize HTTP parameters
HttpParams params = new BasicHttpParams();
ConnManag.........
▪ 程序轨范 程序规范
1、对从页面获得的参数处理——三目运算符。
String id = request.getParameter("id") == null ?
"" : request.getParameter("id").trim();
2、判断是否为数字。
/**
* 判断是否是数字
*
* @param num
.........
[1]ListView的长按菜单_源码分析
来源: 互联网 发布时间: 2014-02-18
ListView的长按菜单___源码分析
ListView的长按菜单___源码分析
Android的listview可以长按弹出来一个菜单。
今天就跟了下代码大概看了下弹出菜单的流程。
我们实现一个菜单长按步骤通常如下:
1.弹出菜单的生成
如果控制listview长按应该生成什么样的菜单。
a、生成一个OnCreateContextMenuListener的接口对象
该接口定义如下:在view.java中
b、实现该接口的onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)函数
在这里,根据menuInfo,通常先转换为AdapterContextMenuInfo,定义如下,在adapterview.java中定义
通过获取MenuItem 的postion进而获取该listitem的信息,进而来决定加入什么样的菜单。
c、将该接口对象设置为listview的listener
2、菜单选择事件的响应
实现该listview的单击响应事件
根据不同的MenuItem的id来进行不同的操作。
二、onCreateContextMenu的调用
那么OnCreateContextMenuListener的onCreateContextMenu是在什么时候调用的呢
是在view.java中createContextMenu函数中调用。
view.createContextMenu是在ContextMenubuilder的show函数调用
而ContextMenubuilder的show函数分别在
PhoneWindow和MidWindow的showContextMenuForChild调用。
再往下今天就没再跟下去了。
三、onCreateContextMenu的生成
在onCreateContextMenu(menu, this, menuInfo)时,传入一个MenuInfo,我们就根据这个MenuInfo来创建响应的菜单。
那这个MenuInfo是怎样生成的呢?
1、首先在View中createContextMenu,调用ContextMenuInfo menuInfo = getContextMenuInfo();
但是getContextMenuInfo()就是返回一个空,得不得我们的数据。
所以,一个view要想能够生成自己的MenuInfo,必须要重新getContextMenuInfo这个函数。
2、在上面的步骤中,我们没有涉及到该函数的重写 ,是因为ListView的父类中,已经重写了改函数 。如下
而这个成员变量的赋值如下:
就是我们在构造菜单时候所用到的ContextMenuInfo.
如果我们要给其它自己定义的View,响应长按菜单。这时候我们就需要重写该View的getContextMenuInfo()。
根据View当前状态生成不同的ContextMenuInfo,进而决定应该弹出什么样的菜单。
ListView的长按菜单___源码分析
Android的listview可以长按弹出来一个菜单。
今天就跟了下代码大概看了下弹出菜单的流程。
我们实现一个菜单长按步骤通常如下:
1.弹出菜单的生成
如果控制listview长按应该生成什么样的菜单。
a、生成一个OnCreateContextMenuListener的接口对象
该接口定义如下:在view.java中
public interface OnCreateContextMenuListener {
/**
* Called when the context menu for this view is being built. It is not
* safe to hold onto the menu after this method returns.
*
* @param menu The context menu that is being built
* @param v The view for which the context menu is being built
* @param menuInfo Extra information about the item for which the
* context menu should be shown. This information will vary
* depending on the class of v.
*/
void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
}
b、实现该接口的onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)函数
在这里,根据menuInfo,通常先转换为AdapterContextMenuInfo,定义如下,在adapterview.java中定义
public static class AdapterContextMenuInfo implements ContextMenu.ContextMenuInfo {
public AdapterContextMenuInfo(View targetView, int position, long id) {
this.targetView = targetView;
this.position = position;
this.id = id;
}
/**
* The child view for which the context menu is being displayed. This
* will be one of the children of this AdapterView.
*/
public View targetView;
/**
* The position in the adapter for which the context menu is being
* displayed.
*/
public int position;
/**
* The row id of the item for which the context menu is being displayed.
*/
public long id;
}
通过获取MenuItem 的postion进而获取该listitem的信息,进而来决定加入什么样的菜单。
c、将该接口对象设置为listview的listener
setOnCreateContextMenuListener(l)
2、菜单选择事件的响应
实现该listview的单击响应事件
public boolean onContextItemSelected(MenuItem item) {}
根据不同的MenuItem的id来进行不同的操作。
二、onCreateContextMenu的调用
那么OnCreateContextMenuListener的onCreateContextMenu是在什么时候调用的呢
是在view.java中createContextMenu函数中调用。
view.createContextMenu是在ContextMenubuilder的show函数调用
而ContextMenubuilder的show函数分别在
PhoneWindow和MidWindow的showContextMenuForChild调用。
再往下今天就没再跟下去了。
三、onCreateContextMenu的生成
在onCreateContextMenu(menu, this, menuInfo)时,传入一个MenuInfo,我们就根据这个MenuInfo来创建响应的菜单。
那这个MenuInfo是怎样生成的呢?
1、首先在View中createContextMenu,调用ContextMenuInfo menuInfo = getContextMenuInfo();
但是getContextMenuInfo()就是返回一个空,得不得我们的数据。
所以,一个view要想能够生成自己的MenuInfo,必须要重新getContextMenuInfo这个函数。
2、在上面的步骤中,我们没有涉及到该函数的重写 ,是因为ListView的父类中,已经重写了改函数 。如下
@Override
protected ContextMenuInfo getContextMenuInfo() {
return mContextMenuInfo;
}
而这个成员变量的赋值如下:
private boolean performLongPress(final View child,final int longPressPosition, final long longPressId) {
boolean handled = false;
if (mOnItemLongClickListener != null) {
handled = mOnItemLongClickListener.onItemLongClick(AbsListView.this, child,
longPressPosition, longPressId);
}
if (!handled) {
mContextMenuInfo = createContextMenuInfo(child, longPressPosition, longPressId);
handled = super.showContextMenuForChild(AbsListView.this);
}
if (handled) {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
return handled;
}
ContextMenuInfo createContextMenuInfo(View view, int position, long id) {
return new AdapterContextMenuInfo(view, position, id);
}
ContextMenuInfo createContextMenuInfo(View view, int position, long id) {
return new AdapterContextMenuInfo(view, position, id);
}
就是我们在构造菜单时候所用到的ContextMenuInfo.
如果我们要给其它自己定义的View,响应长按菜单。这时候我们就需要重写该View的getContextMenuInfo()。
根据View当前状态生成不同的ContextMenuInfo,进而决定应该弹出什么样的菜单。
1 楼
mm4409092
2012-05-05
android listview综合使用示例_结合数据库操作和listitem单击长按等事件处理
http://blog.csdn.net/lk_blog/article/details/7537200
http://blog.csdn.net/lk_blog/article/details/7537200
[2] 多线程施用httpclient
来源: 互联网 发布时间: 2014-02-18
多线程使用httpclient
public class ClientMultiThreadedExecution {
public static void main(String[] args) throws Exception {
// Create and initialize HTTP parameters
HttpParams params = new BasicHttpParams();
ConnManagerParams.setMaxTotalConnections(params, 100);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
// Create and initialize scheme registry
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// Create an HttpClient with the ThreadSafeClientConnManager.
// This connection manager must be used if more than one thread will
// be using the HttpClient.
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(cm, params);
// create an array of URIs to perform GETs on
String[] urisToGet = {
"http://hc.apache.org/",
"http://hc.apache.org/httpcomponents-core/",
"http://hc.apache.org/httpcomponents-client/",
"http://svn.apache.org/viewvc/httpcomponents/"
};
// create a thread for each URI
GetThread[] threads = new GetThread[urisToGet.length];
for (int i = 0; i < threads.length; i++) {
HttpGet httpget = new HttpGet(urisToGet[i]);
threads[i] = new GetThread(httpClient, httpget, i + 1);
}
// start the threads
for (int j = 0; j < threads.length; j++) {
threads[j].start();
}
// join the threads
for (int j = 0; j < threads.length; j++) {
threads[j].join();
}
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
}
/**
* A thread that performs a GET.
*/
static class GetThread extends Thread {
private final HttpClient httpClient;
private final HttpContext context;
private final HttpGet httpget;
private final int id;
public GetThread(HttpClient httpClient, HttpGet httpget, int id) {
this.httpClient = httpClient;
this.context = new BasicHttpContext();
this.httpget = httpget;
this.id = id;
}
/**
* Executes the GetMethod and prints some status information.
*/
@Override
public void run() {
System.out.println(id + " - about to get something from " + httpget.getURI());
try {
// execute the method
HttpResponse response = httpClient.execute(httpget, context);
System.out.println(id + " - get executed");
// get the response body as an array of bytes
HttpEntity entity = response.getEntity();
if (entity != null) {
byte[] bytes = EntityUtils.toByteArray(entity);
System.out.println(id + " - " + bytes.length + " bytes read");
}
} catch (Exception e) {
httpget.abort();
System.out.println(id + " - error: " + e);
}
}
}
}
[3] 程序轨范
来源: 互联网 发布时间: 2014-02-18
程序规范
1、对从页面获得的参数处理——三目运算符。
2、判断是否为数字。
1、对从页面获得的参数处理——三目运算符。
String id = request.getParameter("id") == null ?
"" : request.getParameter("id").trim();
2、判断是否为数字。
/**
* 判断是否是数字
*
* @param num
* @return
*/
public static boolean isNumeric(String num) {
if (num == null || num.length() <= 0) {
return false;
}
if (num.matches("\\d{1,}")) {
return true;
}
return false;
}
//判断是否为手机号
public static boolean isMobile(String mobile) {
boolean tag = true;
// 手机号(正则)
final String pattern1 = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
final Pattern pattern = Pattern.compile(pattern1);
final Matcher mat = pattern.matcher(mobile);
if (!mat.find()) {
tag = false;
}
return tag;
}
最新技术文章: