怎么在Android应用中监听Dialog窗体

发布时间:2020-11-26 15:58:34 作者:Leah
来源:亿速云 阅读:244

怎么在Android应用中监听Dialog窗体?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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布局带

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 将listview显示在dialog当中
  2. jquery easyui1.3.5使用window或dialog窗体时显示错位

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android dialog roi

上一篇:如何在Android中使用PopupWindow制作一个自定义弹窗

下一篇:dumpsys命令怎么在Android项目中使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》