Android Custom Dialog Click the ListView Item event to close dialog

Source: Internet
Author: User

Importandroid.app.Activity;ImportAndroid.app.AlertDialog;ImportAndroid.app.AlertDialog.Builder;ImportAndroid.content.DialogInterface;ImportAndroid.os.Bundle;ImportAndroid.view.Display;Importandroid.view.Gravity;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.view.WindowManager;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ListView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main);} Public voidOnClick (view v) {View View= View.inflate ( This, R.layout.layout_listview,NULL); ListView LV=(ListView) View.findviewbyid (r.id.lv); Lv.setadapter (NewMyadapter ()); Alertdialog.builder Builder=NewBuilder ( This);FinalAlertdialog dialog = Builder.seticon (r.drawable.ic_launcher). Settitle ("Test"). Setview (view). Setpositivebutton ("Off",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface arg0,intarg1) {//TODO auto-generated Method Stub}}). Show (); WindowManager Manager=Getwindowmanager (); Display Display=Manager.getdefaultdisplay ();intwidth =display.getwidth ();intHeight =display.getheight ();d Ialog.getwindow (). setgravity (Gravity.bottom|gravity.left);d Ialog.getwindow (). setlayout (Width/2, HEIGHT/4); Lv.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) {Dialog.dismiss (); Toast.maketext (mainactivity. This, "the dialog box disappears.", Toast.length_short). Show ();}});Private classMyadapterextendsBaseadapter {@Override Public intGetCount () {return5;} @Override PublicObject GetItem (intposition) {return NULL;} @Override Public LongGetitemid (intposition) {return0;} @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {returnView.inflate (mainactivity. This, R.layout.layout_item,NULL);}}}

Layout file 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= "Pop-up dialog box"/></relativelayout>

ListView Layout 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 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>

Personally, this example is suitable for beginners, there is no technical content

Original: http://blog.csdn.net/ydqwym/article/details/9471993

Android Custom Dialog Click the ListView Item event to close dialog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.