歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android自定義對話框的實現

Android自定義對話框的實現

日期:2017/3/1 9:38:00   编辑:Linux編程

Android自定義對話框的思路就是編寫對話框的布局文件xml,然後在對話框中顯示不同的控件。以下以顯示文本控件為例(ImageView等都可以顯示)。

1.布局文件connect_dlg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#ffffffff"
android:orientation="vertical"
android:id="@+id/llToast" >
<TextView
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:textColor="#ffffffff"
android:layout_width="fill_parent"
android:gravity="center"
android:textSize="16sp"
android:background="#FF129de2"
android:id="@+id/tvTitleToast" />
<LinearLayout
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/llToastContent"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_marginBottom="1dip"
android:layout_width="wrap_content"
android:padding="15dip"
android:background="#FFFFFFFF" >
<TextView
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:paddingLeft="10dip"
android:layout_width="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:textColor="#FFff6699"
android:id="@+id/tvTextToast" />
</LinearLayout>
<LinearLayout
android:id="@+id/MyLayout_ad2"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="40sp">
<com.tencent.exmobwin.banner.TAdView
android:id="@+id/adview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top|right" >
</com.tencent.exmobwin.banner.TAdView>
</LinearLayout>
</LinearLayout>

2.編寫顯示對話框函數。ShowConnectDialog(String textString)

private void ShowConnectDialog(String textString) {
LinearLayout loginLayout1 = (LinearLayout) getLayoutInflater().inflate(
R.layout.connect_dlg, null);
// adView.
TextView title = (TextView) loginLayout1
.findViewById(R.id.tvTitleToast);
title.setText("系統提示");
TextView text1 = (TextView) loginLayout1.findViewById(R.id.tvTextToast);
text1.setText(textString);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(loginLayout1);
builder.setPositiveButton("下載MobCtrl服務器?", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//處理確定按鈕

}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 處理取消按鈕
finish();
}
});
builder.create().show();
}

3.顯示對話框。在需要顯示的地方調用即可。

ShowConnectDialog("連接超時,請檢查服務器是否開啟及IP地址是否輸入正確。確保電腦和手機連接在同一個網絡內。");

4.效果

Android 4.4.4 發布下載 http://www.linuxidc.com/Linux/2014-06/103467.htm

最簡單的Ubuntu Touch & Android 雙系統安裝方式 http://www.linuxidc.com/Linux/2014-01/94881.htm

在Nexus上實現Ubuntu和Android 4.4.2 雙啟動 http://www.linuxidc.com/Linux/2014-05/101849.htm

Ubuntu 14.04 配置 Android SDK 開發環境 http://www.linuxidc.com/Linux/2014-05/101039.htm

64位Ubuntu 11.10下Android開發環境的搭建(JDK+Eclipse+ADT+Android SDK詳細) http://www.linuxidc.com/Linux/2013-06/85303.htm

Ubuntu 14.04 x64配置Android 4.4 kitkat編譯環境的方法 http://www.linuxidc.com/Linux/2014-04/101148.htm

Ubuntu 12.10 x64 安裝 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm

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

Copyright © Linux教程網 All Rights Reserved