当前位置:  编程技术>移动开发
本页文章导读:
    ▪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中
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

    
[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、对从页面获得的参数处理——三目运算符。
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;
}

    
最新技术文章:
▪Android实现退出时关闭所有Activity的方法 iis7站长之家
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3