歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android實現對話框-附一個完整例子

Android實現對話框-附一個完整例子

日期:2017/3/1 9:43:08   编辑:Linux編程

Android其實沒有對話框的概念,有的只是用PopupWindow實現一個對話框的效果。下面給一個完整的例子,及用法。

Android中顯示AlertDialog對話框 http://www.linuxidc.com/Linux/2013-12/94015.htm

Android學習筆記之具有選擇功能的對話框 http://www.linuxidc.com/Linux/2013-06/86004.htm

Android全屏對話框(附帶延時關閉效果) http://www.linuxidc.com/Linux/2013-04/82444.htm

Android自定義登陸窗口-對話框 http://www.linuxidc.com/Linux/2013-01/77611.htm

Android-如何關閉AlertDialog.Builder對話框 http://www.linuxidc.com/Linux/2012-09/69489.htm

新建一個PromptWindow類

package com.friendlocation;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class PromptWindow extends PopupWindow
{
// 列表項
Activity context;
View popupView;
OnBtnClickedListener listener;

public PromptWindow(Activity context, int width, int height)
{
super(context);
this.context = context;

// 創建
popupView = context.getLayoutInflater().inflate(R.layout.prompt_window, null);
Button btnOk = (Button) popupView.findViewById(R.id.btn_ok);
btnOk.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
if(listener!= null)
listener.onBtnOkClicked(PromptWindow.this);
}
});
// 顯示對話框
this.setContentView(popupView);
this.setWidth(width);
this.setHeight(height);
setFocusable(true);
setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_menu));
//setBackgroundDrawable(new BitmapDrawable());
}

public interface OnBtnClickedListener
{
public void onBtnOkClicked(PopupWindow win);
}
public void setOnBtnClickedListener(OnBtnClickedListener l)
{
listener = l;
}
}

在res/layout下新建一個布局文件 prompt_window.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:background="@drawable/shape_menu_recent"
android:orientation="vertical"
android:padding="2dp" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#333333"
android:orientation="vertical" >

<TextView
android:id="@+id/menu_title"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#555555"
android:gravity="center"
android:text="注冊提示"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#EEEEEE"
android:textSize="18sp" />

<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="4dp"
android:text="軟件未注冊時限制最多100條信息,請刪除若干消息後繼續使用。或購買注冊碼。"
android:textColor="#DDDDDD"
android:textSize="14sp" />

<Button
android:id="@+id/btn_ok"

android:layout_width="100dp"
android:layout_height="28dp"
android:layout_gravity="right"
android:layout_margin="4dp"
android:background="@drawable/toolbutton"
android:text="點此注冊"
android:textColor="#DDDDDD" />

</LinearLayout>

</LinearLayout>

都看懂了沒?

顯示對話框

PromptWindow win = new PromptWindow(this, 420, 240);
win.showAtLocation(this.findViewById(R.id.titlebar), Gravity.CENTER, 0, 0);
// 點擊按鈕時去干什麼
win.setOnBtnClickedListener(new PromptWindow.OnBtnClickedListener()
{
public void onBtnOkClicked(PopupWindow w)
{
w.dismiss();
//
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivityForResult(intent, 100);
}
});

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved