Android编程实现Dialog窗体监听的方法
2017-03-06 11:31
453 查看
本文实例讲述了Android编程实现Dialog窗体监听的方法。分享给大家供大家参考,具体如下:
今天做了一个Dialong窗体监听包括窗体内的xml监听。
效果图:
test.class代码
package com.test; import Android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.text.Layout; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; public class test extends Activity implements OnClickListener { private ImageButton button=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button=(ImageButton)findViewById(R.id.button); //监听 button.setOnClickListener(this); } @Override public void onClick(View v) { Windows(); } private void Windows() { //接受一些参数如:图片,标题,正文 AlertDialog.Builder builder; AlertDialog alertDialog; //加载和访问资源 Context mContext =test.this; //通过LayoutInlater得到上面xml布局的View view LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE); View layout =inflater.inflate(R.layout.custom_dialog,null); //绑定test和image TextView text=(TextView)layout.findViewById(R.id.text); text.setText("hello"); Button b =(Button)layout.findViewById(R.id.b); b.setText("butto"); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("123"); } }); ImageView image=(ImageView)layout.findViewById(R.id.image); //取出系统自带的图片 image.setImageResource(R.drawable.icon); //加载 builder = new AlertDialog.Builder(mContext); builder.setView(layout); //创建目标,这里创建相应的AlertDialog // alertDialog = builder.create(); //弹出窗口 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }).create(); builder.show(); } }
custom_dialog.xml文件代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF" /> <Button android:id="@+id/b" android:layout_width="wrap_content" android:layout_height="fill_parent" /> </LinearLayout>
main.xml代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ImageButton android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#000000" android:src="@drawable/icon" /> </LinearLayout>
结尾:由于要用到两个xml所以这里有两个xml的代码,一个是点击按钮的xml代码,一个窗体里面的xml布局带
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:
- Android中AlertDialog各种对话框的用法实例详解
- Android入门之AlertDialog用法实例分析
- Android使用自定义alertdialog实现确认退出按钮
- Android中AlertDialog的六种创建方式
- Android AlertDialog自定义样式实现代码
- Android中阻止AlertDialog关闭实例代码
- Android仿IOS自定义AlertDialog提示框
- Android AlertDialog对话框用法示例
- Android使用AlertDialog实现的信息列表单选、多选对话框功能
- Android开发实现AlertDialog中View的控件设置监听功能分析
相关文章推荐
- Android编程之利用服务实现电话监听的方法
- Android编程实现文件浏览功能的方法【类似于FileDialog的功能】
- 关于实现自定义Dialog和实现Dialog里view的事件监听的两种方法
- Android编程实现音量按钮添加监听事件的方法
- java在线聊天项目0.3版本 制作客户端窗体,实现发送按钮和回车发送信息功能,使用ActionListener监听事件中actionPerformed方法(用内部类和匿名内部类两种方法)
- Android编程实现AlertDialog自定义弹出对话框的方法示例
- 三种方法实现Delphi无标题窗体的拖动
- [WPF]WPF实现MDI窗体的方法
- 实现子窗体控制父窗体的方法
- 弹出式窗体的实现方法
- ShowDialog方法运用错误问题,导致传递Owner窗体为空(null)。
- 在DELPHI中实现无标题窗体的移动最简单的方法
- 网络监听的原理、实现技术与防范方法
- C# 非模态窗体(show)实现模态窗体(ShowDialog)的效果
- C#中实现鼠标拖动窗体的方法
- WPF MDI窗体的实现方法
- 在DELPHI中实现无标题窗体的移动最简单的方法
- WPF实现MDI窗体的方法
- VB中利用API函数实现特殊窗体的两种方法
- C#实现窗体淡入淡出效果的几种方法(转)