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的更多相关文章

  1. 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 ...

  2. Javascript中点击(click)事件的3种写法

    方法一 <!DOCTYPE html> <html> <head> <title>Javascript中点击事件方法一</title> &l ...

  3. [Android]解决ClickableSpan中点击后ListView中item的长按冲突的问题

    以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3823429.html 项目中碰到一个问题,情景是这样的: 有一个Lis ...

  4. Android 自定义下拉刷新ListView

    package com.dwtedx.qq.view; import android.content.Context; import android.util.AttributeSet; import ...

  5. Android自定义工具类获取按钮并绑定事件(利用暴力反射和注解)

    Android中为按钮绑定事件的有几种常见方式,你可以在布局文件中为按钮设置id,然后在MainActivity中通过findViewById方法获取按钮对象实例,再通过setOnClickListe ...

  6. Android防止过快点击造成多次事件

    问题 onClick事件是Android开发中最常见的事件. 比方,一个submitButton.功能是点击之后会提交一个订单, 则一般代码例如以下,当中submitOrder()函数会跳转到下一页进 ...

  7. (C#)ListView双击Item事件

    /// <summary> /// 双击选择播放列表项进行播放 /// </summary> /// <param name="sender"> ...

  8. 解决ListView中Item的子控件与Item点击事件冲突

    常常会碰到在ListView中点击当中一个Item.会一并触发其子控件的点击事件.比如Item中的Button.ImageButton等.导致了点击Item中Button以外区域也会触发Button点 ...

  9. ListView的Item动画

    1.效果图 2.需求就是点击item歌曲时,实现一种飞入到预约按钮处的效果 3.思路:在布局文件中加入了一个条目布局,和listView的item一样,点击listView的item时,使用给条目布局 ...

随机推荐

  1. js技巧(一)

    1.文档入口函数 window.onload = function () { //code 将会在页面的全部代码执行完成之后再去执行. } 2.弹窗   alert()   警告窗   confirm ...

  2. JS——轮播图高级版

    需求: 1.页面需要小图标和箭头 2.定时器不断的滑动图片 3.在点击箭头时,图标和图片随即做出响应 核心思想: 1.往左移动到第一张的情况:在第二张移动到第一张时,这个动画效果完成之后,立刻将ul的 ...

  3. html——特殊字符

  4. Ruby开发环境的搭建

    1.Ruby的下载 https://rubyinstaller.org/downloads/ 2.Ruby的安装 3.Eclipse配置Ruby开发环境 插件地址:http://rubyeclipse ...

  5. android studio 控件提示大写

    方法一: 在第一行找到File进入找到setting,找到code completion 右侧复选框 选择-->None—->ok 方法二:<item name="andr ...

  6. java_randomAccessFile类的使用

    RandomAccessFile java提供的对文件内容的访问,既可以读文件,也可以写文件.RandomAccessFile支持随机访问文件,可以访问文件的任意位置 (1)java文件模型 在硬盘上 ...

  7. thinkphp里模版文件js无法使用if condition的问题

    /**     * @example   thinkphp里模版文件js无法使用if condition的问题     * @example  参考地址:https://segmentfault.co ...

  8. 6.3.3 使用 shelve 模块操作二进制文件

    Python标准库shelve也提供了二进制文件操作的功能,可以像字典赋值一样来写入二进制文件,也可以像字典一样读取二进制文件,有点类似于NoSQL数据库MongoDB. import shelve ...

  9. 【codeforces 768E】Game of Stones

    [题目链接]:http://codeforces.com/contest/768/problem/E [题意] NIM游戏的变种; 要求每一堆石头一次拿了x个之后,下一次就不能一次拿x个了; 问你结果 ...

  10. ClassCastException:ColorDrawable cannot be cast to RoundRectDrawableWithShadow

    错误信息 java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android ...