android控件封裝 自己封裝的dialog控件
先上圖:

主activity
package com.su.testcustomdialog;
import com.su.testcustomdialog.MyDialog.Dialogcallback;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CustomDialogActivity extends Activity {
private TextView textView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView11);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDialog myDialog = new MyDialog(CustomDialogActivity.this);
myDialog.setContent("哥來(lái)自Activity");
myDialog.setDialogCallback(dialogcallback);
myDialog.show();
}
});
}
/**
* 設(shè)置mydialog需要處理的事情
*/
Dialogcallback dialogcallback = new Dialogcallback() {
@Override
public void dialogdo(String string) {
textView.setText("哥來(lái)自Dialog: " + string);
}
};
}
然后是MyDialog的核心了
package com.su.testcustomdialog;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* 自定義dialog
* @author sfshine
*
*/
public class MyDialog {
Context context;
Dialogcallback dialogcallback;
Dialog dialog;
Button sure;
TextView textView;
EditText editText;
/**
* init the dialog
* @return
*/
public MyDialog(Context con) {
this.context = con;
dialog = new Dialog(context, R.style.dialog);
dialog.setContentView(R.layout.dialog);
textView = (TextView) dialog.findViewById(R.id.textview);
sure = (Button) dialog.findViewById(R.id.button1);
editText = (EditText) dialog.findViewById(R.id.editText1);
sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogcallback.dialogdo(editText.getText().toString());
dismiss();
}
});
}
/**
* 設(shè)定一個(gè)interfack接口,使mydialog可以處理activity定義的事情
* @author sfshine
*
*/
public interface Dialogcallback {
public void dialogdo(String string);
}
public void setDialogCallback(Dialogcallback dialogcallback) {
this.dialogcallback = dialogcallback;
}
/**
* @category Set The Content of the TextView
* */
public void setContent(String content) {
textView.setText(content);
}
/**
* Get the Text of the EditText
* */
public String getText() {
return editText.getText().toString();
}
public void show() {
dialog.show();
}
public void hide() {
dialog.hide();
}
public void dismiss() {
dialog.dismiss();
}
}
dialog的布局
<?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="wrap_content"
android:layout_margin="30.0dp"
android:orientation="vertical"
android:padding="10dip" >
<!-- 這里如果使用android:layout_width="5000dip"設(shè)置一個(gè)極大的值 系統(tǒng)就會(huì) -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30.0dp"
android:gravity="center"
android:text="自定義Dialog"
android:textColor="#F0F"
android:textSize="20dip" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="確定" />
</LinearLayout>
style 的文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">#FFF</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
</resources>
- Android中DialogFragment自定義背景與寬高的方法
- Android開(kāi)發(fā)之基于DialogFragment創(chuàng)建對(duì)話框的方法示例
- 詳解Android應(yīng)用中DialogFragment的基本用法
- Android中使用DialogFragment編寫(xiě)對(duì)話框的實(shí)例教程
- android中ProgressDialog與ProgressBar的使用詳解
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- Android中Dialog去黑邊的方法
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- android dialog自定義實(shí)例詳解
- Android開(kāi)發(fā)之DialogFragment用法實(shí)例總結(jié)
相關(guān)文章
Android之高德地圖定位SDK集成及地圖功能實(shí)現(xiàn)
本文主要介紹了Android中高德地圖定位SDK集成及地圖功能的實(shí)現(xiàn)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04Android實(shí)現(xiàn)可拖拽列表和多選功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可拖拽列表和多選功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06Android Studio中導(dǎo)入JNI生成的.so庫(kù)的實(shí)現(xiàn)方法
這篇文章主要介紹了Android Studio中導(dǎo)入JNI生成的.so庫(kù)的實(shí)現(xiàn)方法的相關(guān)資料,這里不僅提供實(shí)現(xiàn)方案并提供了實(shí)現(xiàn)的方法,需要的朋友可以參考下2017-07-07Android Toast使用的簡(jiǎn)單小結(jié)(推薦)
這篇文章主要介紹了Android Toast使用的簡(jiǎn)單小結(jié),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03android基于SwipeRefreshLayout實(shí)現(xiàn)類(lèi)QQ的側(cè)滑刪除
本篇文章主要介紹了android基于SwipeRefreshLayout實(shí)現(xiàn)類(lèi)QQ的側(cè)滑刪除,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10利用Kotlin如何實(shí)現(xiàn)Android開(kāi)發(fā)中的Parcelable詳解
這篇文章主要給大家介紹了關(guān)于利用Kotlin如何實(shí)現(xiàn)Android開(kāi)發(fā)中的Parcelable的相關(guān)資料,并且給大家介紹了關(guān)于Kotlin使用parcelable出現(xiàn):BadParcelableException: Parcelable protocol requires a Parcelable.Creator...問(wèn)題的解決方法,需要的朋友可以參考下。2017-12-12RecyclerView實(shí)現(xiàn)水波紋點(diǎn)擊效果
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)水波紋點(diǎn)擊效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android RecyclerView加載不同布局簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView加載不同布局簡(jiǎn)單實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-08-08Flutter 包管理器和資源管理使用學(xué)習(xí)
這篇文章主要為大家介紹了Flutter 包管理器和資源管理使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12