android控件封装 自己封装的dialog控件
程序员文章站
2023-12-09 15:20:51
自定义dialog肯定是用的很多了但是感觉每次做都是很乱 单纯完成任务而已,现在封装了一下 以后用到直接copy 先上图: 主activity 复制代码 代码如下: pac...
自定义dialog肯定是用的很多了但是感觉每次做都是很乱 单纯完成任务而已,现在封装了一下 以后用到直接copy
先上图:
主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("哥来自activity");
mydialog.setdialogcallback(dialogcallback);
mydialog.show();
}
});
}
/**
* 设置mydialog需要处理的事情
*/
dialogcallback dialogcallback = new dialogcallback() {
@override
public void dialogdo(string string) {
textview.settext("哥來自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();
}
});
}
/**
* 设定一个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"设置一个极大的值 系统就会 -->
<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>
先上图:
主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("哥来自activity");
mydialog.setdialogcallback(dialogcallback);
mydialog.show();
}
});
}
/**
* 设置mydialog需要处理的事情
*/
dialogcallback dialogcallback = new dialogcallback() {
@override
public void dialogdo(string string) {
textview.settext("哥來自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();
}
});
}
/**
* 设定一个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"设置一个极大的值 系统就会 -->
<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>