Android編程實(shí)現(xiàn)Dialog窗體監(jiān)聽的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)Dialog窗體監(jiān)聽的方法。分享給大家供大家參考,具體如下:
今天做了一個(gè)Dialong窗體監(jiān)聽包括窗體內(nèi)的xml監(jiān)聽。
效果圖:

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);
//監(jiān)聽
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Windows();
}
private void Windows() {
//接受一些參數(shù)如:圖片,標(biāo)題,正文
AlertDialog.Builder builder;
AlertDialog alertDialog;
//加載和訪問(wèn)資源
Context mContext =test.this;
//通過(guò)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);
//取出系統(tǒng)自帶的圖片
image.setImageResource(R.drawable.icon);
//加載
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
//創(chuàng)建目標(biāo),這里創(chuàng)建相應(yīng)的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>
結(jié)尾:由于要用到兩個(gè)xml所以這里有兩個(gè)xml的代碼,一個(gè)是點(diǎn)擊按鈕的xml代碼,一個(gè)窗體里面的xml布局帶
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問(wèn)題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android入門之AlertDialog用法實(shí)例分析
- Android使用自定義alertdialog實(shí)現(xiàn)確認(rèn)退出按鈕
- Android中AlertDialog的六種創(chuàng)建方式
- Android AlertDialog自定義樣式實(shí)現(xiàn)代碼
- Android中阻止AlertDialog關(guān)閉實(shí)例代碼
- Android仿IOS自定義AlertDialog提示框
- Android AlertDialog對(duì)話框用法示例
- Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對(duì)話框功能
- Android開發(fā)實(shí)現(xiàn)AlertDialog中View的控件設(shè)置監(jiān)聽功能分析
相關(guān)文章
Android之PreferenceActivity應(yīng)用詳解
為了引入這個(gè)概率 首先從需求說(shuō)起 即:現(xiàn)有某Activity專門用于手機(jī)屬性設(shè)置 那么應(yīng)該如何做呢2012-11-11
簡(jiǎn)單實(shí)現(xiàn)Android數(shù)獨(dú)游戲
這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)Android數(shù)獨(dú)游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android ImageView Src 和Background 區(qū)別
這篇文章主要介紹了Android ImageView Src 和Background 區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android用Scroller實(shí)現(xiàn)一個(gè)可向上滑動(dòng)的底部導(dǎo)航欄
本篇文章主要介紹了Android用Scroller實(shí)現(xiàn)一個(gè)可上滑的底部導(dǎo)航欄,具有一定的參考價(jià)值,有興趣的小伙伴們可以參考一下2017-07-07
Android下拉刷新SwipeRefreshLayout控件使用方法
這篇文章主要介紹了Android下拉刷新SwipeRefreshLayout控件使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android TV 焦點(diǎn)框移動(dòng)的實(shí)現(xiàn)方法
本篇文章主要介紹了Android TV 焦點(diǎn)框移動(dòng)的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Android編程實(shí)現(xiàn)的簡(jiǎn)易路徑導(dǎo)航條功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)的簡(jiǎn)易路徑導(dǎo)航條功能,結(jié)合實(shí)例形式分析了Android結(jié)合視圖、布局及路徑操作實(shí)現(xiàn)導(dǎo)航條功能的相關(guān)技巧,需要的朋友可以參考下2017-01-01
基于SurfaceView實(shí)現(xiàn)可拖動(dòng)視頻控件
這篇文章主要為大家詳細(xì)介紹了基于SurfaceView的可拖動(dòng)視頻控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04

