Android简易实战教程--第二十二话《自定义组合控件模拟qq登录下拉框和其中的一些”小技巧”》
转载此文章请注明出处:点击打开链接 http://blog.csdn.net/qq_32059827/article/details/52313516
首先,很荣幸此专栏能被CSDN推荐到主页。荣幸的同时,也激励自己会把这个专栏一直更新下去。
进入今天的主题:
我们在qq登录的时候,会有一个下拉的按钮,来查看历史登录账号。这一篇就模拟这个效果,自定义组合框实现之。
这里面会用到popupwindow,对于popupwindow的原始用法欢迎看之前的一篇文章,对弹出窗体做过介绍:点击打开链接
今天不再使用那种方式来定义了,使用其他的api,实现方式更简单。同时,有一些”小技巧”在里面。
定义主布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center_horizontal"
android:layout_height="match_parent"> <EditText
android:id="@+id/et_number"
android:layout_width="200dip"
android:layout_height="wrap_content" /> <ImageButton
android:id="@+id/ib_down_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/et_number"
android:layout_alignRight="@id/et_number"
android:layout_alignTop="@id/et_number"
android:layout_marginRight="5dip"
android:background="@drawable/down_arrow" /> </RelativeLayout>
小技巧:图片的点击事件ImageButton能更好的胜任这个工作,但是使用它的时候,需要把背景去掉(ImageButton默认是携带一个背景的)。设置背景无的方式有两种:1、加入两个属性: android:background="@android:color/transparent" 作用是让背景为透明状态 android:src="@drawable/icon_home" ; 2直接设置android:background="@drawable/down_arrow" 。一般对ImageButton图片使用第二种。
弹出窗体,窗体需要一个布局。显然,要实现模拟qq下拉需要用listview显示,它的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
android:padding="5dip" >
<!-- android:descendantFocusability="blocksDescendants"表示让后辈只在自己那一块位置响应事件,不要抢占父组件本身的事件 -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/user" /> <TextView
android:id="@+id/tv_listview_item_number"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_weight="1"
android:gravity="center"
android:text="默认的号码"
android:textSize="18sp" /> <ImageButton
android:id="@+id/ib_listview_item_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/delete" /> </LinearLayout>
小技巧:注意在根布局里面加入的android:descendantFocusability="blocksDescendants"。如果去掉的话,当在点击listview的item的时候,事件会被他上边的ImageButton抢占走。加入它的目的就是:让LiearLayout的子组件只在自己那一块位置响应事件,不要抢占父组件本身的事件。
接下来,就是业务逻辑,如下:
package com.itydl.popupwindowdemo; import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener, OnItemClickListener { private List<String> numberList; // 号码集合
private ListView mListView;
private EditText etNumber;
private PopupWindow popupWindow;
private NumberAdapter mAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); etNumber = (EditText) findViewById(R.id.et_number);
ImageButton ibArrow = (ImageButton) findViewById(R.id.ib_down_arrow); ibArrow.setOnClickListener(this);
} @Override
public void onClick(View v) {
showPopupWindow();
} /**
* 弹出一个下拉窗体
*/
private void showPopupWindow() {
initListView(); // 创建PopupWindow对象,指定内容为ListView,宽度为输入框的宽度,高度为200,当前窗体可以获取焦点
popupWindow = new PopupWindow(mListView, etNumber.getWidth() - 8, 200, true); /*************************************** 这里需要添加两行代码 ***************************************************************/ /**
* View anchor, 相对父组件(这里的父组件是一个编辑框)
* int xoff, x偏移量
* int yoff,y偏移量
* 说明:弹出窗体右上角与父组件左下角作为相对位置。
*/
popupWindow.showAsDropDown(etNumber, 4, -4);//表示mListView的窗体,相对父组件左移4个像素,上移4个像素
} /**
* 初始化ListView
*/
private void initListView() {
mListView = new ListView(this);
/****/
mListView.setOnItemClickListener(this); numberList = new ArrayList<String>();
for (int i = 0; i < 30; i++) {
//数据源添加数据
numberList.add("10000060" + i);
} mAdapter = new NumberAdapter();
mListView.setAdapter(mAdapter);
} class NumberAdapter extends BaseAdapter { @Override
public int getCount() {
return numberList.size();
} @Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.listview_item, null);
} TextView tvNumber = (TextView) convertView.findViewById(R.id.tv_listview_item_number);
ImageButton ibDelete = (ImageButton) convertView.findViewById(R.id.ib_listview_item_delete);
tvNumber.setText(numberList.get(position)); ibDelete.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
numberList.remove(position);//移除数据源位置
mAdapter.notifyDataSetChanged();//通知listview刷新
}
}); return convertView;
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} } @Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// System.out.println("onItemClick");
//获取点击那项的数据
String number = numberList.get(position);
etNumber.setText(number);
popupWindow.dismiss(); //关闭窗体
}
}
可以运行程序看看效果了:
基本实现了需求,但是最后还有几个小技巧要说一下:
1、您会发现等弹出窗体后,没办法关闭窗体,按返回键都关闭不掉,这个时候就需要设置两行代码解决这个问题。需要在哪里加入我已经在代码中标注出来了,在那个地方加入两行代码:
popupWindow.setOutsideTouchable(true);//popupWindow可以接收其它位置的点击事件触发
popupWindow.setBackgroundDrawable(new BitmapDrawable());/
2、如果想干掉listview旁边的下拉滚动条,只需要加入下边一行代码即可:
mListView.setVerticalScrollBarEnabled(false);
到此,小demo完毕了。
欢迎关注本博客点击打开链接 http://blog.csdn.net/qq_32059827,每天花上5分钟,阅读一篇有趣的安卓小文哦。
Android简易实战教程--第二十二话《自定义组合控件模拟qq登录下拉框和其中的一些”小技巧”》的更多相关文章
- 自定义SWT控件四之其它下拉框
4.其它下拉框 4.1 添加联动二级多选择框(有添加按钮和删除按钮) package com.view.control.select; import java.util.ArrayList; impo ...
- Android简易实战教程--第二十九话《创建图片副本》
承接第二十八话加载大图片,本篇介绍如何创建一个图片的副本. 安卓中加载的原图是无法对其修改的,因为默认权限是只读的.但是通过创建副本,就可以对其做一些修改,绘制等了. 首先创建一个简单的布局.一个放原 ...
- Android简易实战教程--第二十六话《网络图片查看器在本地缓存》
本篇接第二十五话 点击打开链接 http://blog.csdn.net/qq_32059827/article/details/52389856 上一篇已经把王略中的图片获取到了.生活中有这么 ...
- Android简易实战教程--第二十话《通过广播接收者,对拨打电话外加ip号》
没睡着觉,起来更篇文章吧哈哈!首先祝贺李宗伟击败我丹,虽然我是支持我丹的,但是他也不容易哈哈,值得尊敬的人!切入正题:这一篇来介绍个自定义广播接收者. 通常我们在外拨电话的时候,一般为使用网络电话.如 ...
- Android简易实战教程--第二十八话《加载大图片》
Android系统以ARGB表示每个像素,所以每个像素占用4个字节,很容易内存溢出.假设手机内存比较小,而要去加载一张像素很高的图片的时候,就会因为内存不足导致崩溃.这种异常是无法捕获的 内存不足并不 ...
- Android简易实战教程--第二十五话《网络图片查看器》
访问网络已经有了很成熟的框架.这一篇只是介绍一下HttpURLConnection的简单用法,以及里面的"注意点".这一篇可以复习或者学习HttpURLConnection.han ...
- Android简易实战教程--第二十四话《画画板》
今天完成一个画画板. 首先来个布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- Android简易实战教程--第二十一话《内容观察者监听数据库变化》
当数据库的数据发生改变,我们又想知道具体改变的情况时,就需要对数据库的变化情况做一个监控.这个任务,就由内容观察者来完成.下面这个案例,为短信数据库注册内容观察者,来监控短信的变化情况,当短信数据库发 ...
- Android简易实战教程--第十二话《代码获取手机总运行内存的大小》
手机RAM存储,类似于电脑的内存.这一篇,对通过代码获取手机总内存大小做详细介绍. 首先,定义一个engine类,这个类功能就是获取进程信息,包括运行的程序个数,系统总内存,系统剩余总内存.本篇先完成 ...
随机推荐
- codevs 3061 质子撞击炮②
提交地址:http://codevs.cn/problem/3016/ 3016 质子撞击炮 II 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描 ...
- 基于webpack的React项目搭建(二)
前言 前面我们已经搭建了基础环境,现在将开发环境更完善一些. devtool 在开发的过程,我们会经常调试,so,为了方便我们在chrome中调试源代码,需要更改webpack.config.js,然 ...
- [NOIp 2011]Mayan游戏
Description Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏 ...
- [HNOI2010]STONE取石头游戏
题目描述 A 公司正在举办一个智力双人游戏比赛----取石子游戏,游戏的获胜者将会获得 A 公司提供的丰厚奖金,因此吸引了来自全国各地的许多聪明的选手前来参加比赛. 与经典的取石子游戏相比,A公司举办 ...
- HDU3389 Game
Problem Description Bob and Alice are playing a new game. There are n boxes which have been numbered ...
- ●BZOJ 1767 [Ceoi2009]harbingers
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1767 题解: 斜率优化DP,单调栈,二分 定义 DP[i] 表示从 i 节点出发,到达根所花 ...
- BZOJ1095(动态点分治+堆)
终于把这个坑填了.. 按重心分治建树,每个点存两个堆,第一个存的是这个点子树中的点到父重心的距离,第二个存的是子节点第一个堆的堆顶,同时有一个全局答案堆,存的是每个点第二个堆的最大值+次大值. 20亿 ...
- 试说明采用双缓冲技术如何进行I/O操作
输入设备先将第一个缓冲区装满数据,在输入设备向第二个缓冲区装数据时,处理机就可以从第一个缓冲区取出数据进行处理:当一个缓冲区的数据处理完毕,若第二个缓冲区已经装满,则处理机又可以从第二个缓冲区取出数据 ...
- iOS 隐藏导航条分割线
// 导航条分割线 @property (nonatomic, strong) UIView *navSeparateView; // 获取导航条分割线 UIView *backgroundView ...
- 解决ansible首次连接host服务器需验证问题
问题描述: [root@iZm5e79rtwsq2hm57teyk5Z ansible]# ansible aofeng -f 5 -m ping 47.93.18.191 | FAILED! =&g ...