package com.fermax.test;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* 实现原理
* 定义一个接口,在activity中实现。把接口引用传给dialog。dialog通过接口引用,调用activity中方法
* @author Administrator
*/
public class TestDialogActivity extends Activity{
private TextView t;
Button dlg;
private MyInterface myListener = new MyInterface(){
@Override
public void method(String text){
t.setText(text);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_dialog);
t = (TextView) findViewById(R.id.result);
dlg = (Button) findViewById(R.id.dlg);
dlg.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
new LoginDialog(TestDialogActivity.this, myListener).setDisplay();
}
});
}
//对话框
class LoginDialog extends Dialog {
private Button yesButton;
MyInterface myInterface;
public LoginDialog(Context context, MyInterface myInterface) {
super(context);
this.myInterface = myInterface;
}
public void setDisplay() {
setContentView(R.layout.layout_login_dialog);
yesButton = (Button) findViewById(R.id.btn_login);
yesButton.setOnClickListener(yesClickListener);
setCancelable(false);
show();
}
View.OnClickListener yesClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
myInterface.method("数据");
dismiss();
}
};
}
//接口
interface MyInterface {
public void method(String s);
}
}
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* 实现原理
* 定义一个接口,在activity中实现。把接口引用传给dialog。dialog通过接口引用,调用activity中方法
* @author Administrator
*/
public class TestDialogActivity extends Activity{
private TextView t;
Button dlg;
private MyInterface myListener = new MyInterface(){
@Override
public void method(String text){
t.setText(text);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_dialog);
t = (TextView) findViewById(R.id.result);
dlg = (Button) findViewById(R.id.dlg);
dlg.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
new LoginDialog(TestDialogActivity.this, myListener).setDisplay();
}
});
}
//对话框
class LoginDialog extends Dialog {
private Button yesButton;
MyInterface myInterface;
public LoginDialog(Context context, MyInterface myInterface) {
super(context);
this.myInterface = myInterface;
}
public void setDisplay() {
setContentView(R.layout.layout_login_dialog);
yesButton = (Button) findViewById(R.id.btn_login);
yesButton.setOnClickListener(yesClickListener);
setCancelable(false);
show();
}
View.OnClickListener yesClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
myInterface.method("数据");
dismiss();
}
};
}
//接口
interface MyInterface {
public void method(String s);
}
}