android自定义dialog中点击listview的item事件关闭dialog
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void onClick(View v) {
View view = View.inflate(this, R.layout.layout_listview, null);
ListView lv = (ListView) view.findViewById(R.id.lv);
lv.setAdapter(new MyAdapter());
AlertDialog.Builder builder = new Builder(this);
final AlertDialog dialog = builder.setIcon(R.drawable.ic_launcher).setTitle("测试")
.setView(view).setPositiveButton("关闭", new DialogInterface.OnClickListener(){ @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub } }).show(); WindowManager manager = getWindowManager();
Display display = manager.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
dialog.getWindow().setGravity(Gravity.BOTTOM |Gravity.LEFT);
dialog.getWindow().setLayout(width/2, height/4);
lv.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { dialog.dismiss();
Toast.makeText(MainActivity.this, "对话框消失了", Toast.LENGTH_SHORT).show();
}
});
} private class MyAdapter extends BaseAdapter { @Override
public int getCount() { return 5;
} @Override
public Object getItem(int position) { return null;
} @Override
public long getItemId(int position) { return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
return View.inflate(MainActivity.this, R.layout.layout_item, null); } }
}
布局文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="弹出对话框" /> </RelativeLayout>
ListView布局layout_listview.xml
<?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="match_parent"
android:orientation="vertical" > <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </LinearLayout>
listview的item布局layout_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="0dip"
android:orientation="horizontal" > <TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="AAA" /> <TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BBB" /> <TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CCC" /> <TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="DDD" /> </LinearLayout>
个人认为,这个例子适合初学者,没有什么技术含量
原文:http://blog.csdn.net/ydqwym/article/details/9471993
android自定义dialog中点击listview的item事件关闭dialog的更多相关文章
- Android Dialogs(5)[正常显示dlg,将Fragment显示为dialog,将Aty显示为dlg,嵌入],关闭Dialog
Showing a Dialog When you want to show your dialog, create an instance of your DialogFragment and ca ...
- Javascript中点击(click)事件的3种写法
方法一 <!DOCTYPE html> <html> <head> <title>Javascript中点击事件方法一</title> &l ...
- [Android]解决ClickableSpan中点击后ListView中item的长按冲突的问题
以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3823429.html 项目中碰到一个问题,情景是这样的: 有一个Lis ...
- Android 自定义下拉刷新ListView
package com.dwtedx.qq.view; import android.content.Context; import android.util.AttributeSet; import ...
- Android自定义工具类获取按钮并绑定事件(利用暴力反射和注解)
Android中为按钮绑定事件的有几种常见方式,你可以在布局文件中为按钮设置id,然后在MainActivity中通过findViewById方法获取按钮对象实例,再通过setOnClickListe ...
- Android防止过快点击造成多次事件
问题 onClick事件是Android开发中最常见的事件. 比方,一个submitButton.功能是点击之后会提交一个订单, 则一般代码例如以下,当中submitOrder()函数会跳转到下一页进 ...
- (C#)ListView双击Item事件
/// <summary> /// 双击选择播放列表项进行播放 /// </summary> /// <param name="sender"> ...
- 解决ListView中Item的子控件与Item点击事件冲突
常常会碰到在ListView中点击当中一个Item.会一并触发其子控件的点击事件.比如Item中的Button.ImageButton等.导致了点击Item中Button以外区域也会触发Button点 ...
- ListView的Item动画
1.效果图 2.需求就是点击item歌曲时,实现一种飞入到预约按钮处的效果 3.思路:在布局文件中加入了一个条目布局,和listView的item一样,点击listView的item时,使用给条目布局 ...
随机推荐
- Hash二次探测
Hash的二次探测,当hash的长度为n:插入val,当Hash[val]不为0时,选择新地址newval = val +(-) 1*1,val+(-)2*2,val+(-)(n-1)*(n-1); ...
- 集合Set、List、Map的遍历方法
package com.shellway.javase; import java.util.ArrayList; import java.util.Collection; import java.ut ...
- (转)Hibernate框架基础——在Hibernate中java对象的状态
http://blog.csdn.net/yerenyuan_pku/article/details/52760627 在Hibernate中java对象的状态 Hibernate把对象分为4种状态: ...
- Oracle行转列/列转行
1.oracle的pivot函数 原表 使用pivot函数: with temp as(select '四川省' nation ,'成都市' city,'第一' ranking from dual u ...
- 本地文件与服务器文件同步shell脚本
为何创建此脚本是因为方便项目的布署,不需要手动地去同步不同的项目,而只需要简单的执行shell脚本,输入项目名就能发布到服器上. 1.shell文件代码 #!/bin/sh read -t 30 -p ...
- c/c++排坑(3) -- c/c++中的switch语句
switch语句的简单介绍 一个 switch 语句允许测试一个变量等于多个值时的情况.每个值称为一个 case,且被测试的变量会对每个 switch case 进行检查. switch(expres ...
- Problem 21
Problem 21 https://projecteuler.net/problem=21 Let d(n) be defined as the sum of proper divisors of ...
- PAT 1098. Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 9.3.4 BeaufitulSoup4
BeautifulSoup 是一个非常优秀的Python扩展库,可以用来从HTML或XML文件中提取我们感兴趣的数据,并且允许指定使用不同的解析器. 使用 pip install BeaufifulS ...
- 在 ServiceModel 客户端配置部分中,找不到引用协定“XXX”的默认终结点元素
一.问题 在调用远程web services接口时出现了以下问题: 二.可能的原因和解决方法 网站根目录里的web.config文件缺少了相应的配置信息 <?xml version=" ...