【Android】自定义Dialog

先上图

main.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:orientation="vertical" >

<Button
android:id="@+id/mydialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dialog" />

</LinearLayout>

 

CustomDialogActivity.java主界面后台代码

import com.example.viewpagerdemo.MyDialog.Dialogcallback;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CustomDialogActivity extends Activity {
private Button mydialog;
private TextView textView;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mydialog = (Button) findViewById(R.id.mydialog);
mydialog.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
MyDialog myDialog = new MyDialog(CustomDialogActivity.this);
myDialog.setContent("しばらくお待ち下さい。");
myDialog.setDialogCallback(dialogcallback);
myDialog.show();
}
});
}

Dialogcallback dialogcallback = new Dialogcallback() {
@Override
public void dialogdo(String string) {
textView.setText("哥來自Dialog: " + string);
}
};
}

 

dialog.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="310dp"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp"
android:layout_gravity="center_horizontal"
android:text="iRemoconを検索中"
android:textColor="#666666"
android:textSize="18dp" />

<ImageView
android:layout_width="280dp"
android:layout_height="150dp"
android:background="#cccccc"
android:layout_gravity="center_horizontal"/>

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="しばらくお待ち下さい。"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:textColor="#666666"
android:textSize="14dp"/>

<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cccccc"/>

<TextView
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="キャンセル"
android:textSize="14dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:textColor="#208bd0"/>

</LinearLayout>

 

MyDialog.java核心文件

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MyDialog {
Context context;
Dialogcallback dialogcallback;
Dialog dialog;
TextView sure;
TextView textView;
EditText editText;

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 = (TextView) dialog.findViewById(R.id.button1);
sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}

public interface Dialogcallback {
public void dialogdo(String string);
}

public void setDialogCallback(Dialogcallback dialogcallback) {
this.dialogcallback = dialogcallback;
}

public void setContent(String content) {
textView.setText(content);
}

public String getText() {
return editText.getText().toString();
}

public void show() {
dialog.show();
}

public void hide() {
dialog.hide();
}

public void dismiss() {
dialog.dismiss();
}
}

 

style.xml文件

<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>

posted @ 2015-10-08 11:24  改变世界的老十七  阅读(222)  评论(0编辑  收藏  举报