Android编程实现Dialog窗体监听的方法

 更新时间:2017年03月06日 11:31:48   作者:罗瑜  
这篇文章主要介绍了Android编程实现Dialog窗体监听的方法,结合实例形式分析了Android针对Dialog对话框窗体事件监听与响应相关操作技巧,需要的朋友可以参考下

本文实例讲述了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使用Kotlin API实践WorkManager

    Android使用Kotlin API实践WorkManager

    这篇文章主要介绍了Android使用Kotlin API实践WorkManager的步骤,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-04-04
  • Android Jetpack架构组件 ViewModel详解

    Android Jetpack架构组件 ViewModel详解

    这篇文章主要介绍了Android Jetpack架构组件 ViewModel详解,ViewModel类让数据可在发生屏幕旋转等配置更改后继续存在,ViewModel类旨在以注重生命周期的方式存储和管理界面相关的数据。感兴趣可以来学习一下
    2020-07-07
  • Android中使用ViewFlipper进行手势切换实例

    Android中使用ViewFlipper进行手势切换实例

    这篇文章主要介绍了Android中使用ViewFlipper进行手势切换的方法,以实例形式详细讲述了XML文件的定义及功能函数的实现过程,需要的朋友可以参考下
    2014-10-10
  • Kotlin协程flowOn与线程切换超详细示例介绍

    Kotlin协程flowOn与线程切换超详细示例介绍

    这篇文章主要介绍了Kotlin协程flowOn与线程切换,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-09-09
  • Ubuntu中为Android简单介绍硬件抽象层(HAL)

    Ubuntu中为Android简单介绍硬件抽象层(HAL)

    本文主要介绍在Android 的硬件抽象层,学习Android 硬件抽象层(HAL)对理解整个Android都是有非常大的作用,有兴趣的小伙伴可以参考下
    2016-08-08
  • Android实现长图文截图功能实例代码

    Android实现长图文截图功能实例代码

    这篇文章主要给大家介绍了关于Android实现长图文截图功能的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • Dagger2 Android依赖注入学习笔记

    Dagger2 Android依赖注入学习笔记

    这篇文章主要介绍了Dagger2 Android依赖注入学习笔记,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • Android中AsyncTask的入门使用学习指南

    Android中AsyncTask的入门使用学习指南

    AsyncTask异步任务,用于执行耗时任务并在UI线程中更新结果。下面这篇文章主要给大家介绍了关于Android中AsyncTask入门使用的相关资料,需要的朋友可以参考下
    2019-02-02
  • android实现人脸识别技术的示例代码

    android实现人脸识别技术的示例代码

    本篇文章主要介绍了android人脸识别技术的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Android普通对话框用法实例分析

    Android普通对话框用法实例分析

    这篇文章主要介绍了Android普通对话框用法,以实例形式较为详细的分析了Android对话框的创建技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09

最新评论