android控件封装 自己封装的dialog控件
内容摘要
自定义dialog肯定是用的很多了但是感觉每次做都是很乱 单纯完成任务而已,现在封装了一下 以后用到直接copy 先上图: 主activity 复制代码 代码如下: package com.su.testcust
文章正文
自定义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>
代码注释