我的界面:自定義對話框構造
public interface LoginDialogDismissListener {
void loginDialogCancel();
void loginDialogSuccess();
}
我的活動:
public class MainActivity extends Activity implements LoginDialogDimissListener {
public void loginDialogCancel() {
//do stuff here
}
public void loginDialogSuccess() {
//do some other stuff here!
}
}
我一個LoginDialog:
public class LoginDialog extends Dialog implements OnClickListener, LoginChecker {
private ProgressBar pbLogin;
private TextView tvLoginstatus;
private Button cancel;
private Button save;
private EditText username;
private EditText password;
public LoginDialog(Context context) {
super(context);
setContentView(R.layout.login_dialog);
pbLogin = (ProgressBar) findViewById(R.id.progressBarLogin);
tvLoginstatus = (TextView) findViewById(R.id.dialogTvLoginstatus);
cancel = (Button) findViewById(R.id.bDialogCancel);
save = (Button) findViewById(R.id.bDialogSave);
username = (EditText) findViewById(R.id.dialogEtUsername);
password = (EditText) findViewById(R.id.dialogEtPassword);
setTitle("Brukerdata");
pbLogin.setVisibility(View.INVISIBLE);
tvLoginstatus.setVisibility(View.INVISIBLE);
cancel.setOnClickListener(this);
save.setOnClickListener(this);
//setOnDismissListener(this);
}
public void onClick(View v) {
if (v.getId() == R.id.bDialogSave) {
saveClick();
}
else if (v.getId() == R.id.bDialogCancel) {
cancelClick();
}
}
private void saveClick() {
save.setEnabled(false);
String[] credentials = {username.getText().toString(), password.getText().toString()};
pbLogin.setVisibility(View.VISIBLE);
CheckLoginTask logintask = new CheckLoginTask(this, credentials, 0);
logintask.execute();
}
private void cancelClick() {
this.dismiss();
//Here I want to call LoginDialogDismissListener.loginDialogCancel()
}
//Called from my AsyncTask (CheckLoginTask)
public void onLoginSuccess(int requestCode) {
this.dismiss();
//Here I want to call LoginDialogDismissListener.loginDialogSuccess()
}
//Called from my AsyncTask (CheckLoginTask)
public void onLoginFail(int requestCode) {
pbLogin.setVisibility(View.INVISIBLE);
tvLoginstatus.setText("Feil brukernavn/passord ...");
tvLoginstatus.setVisibility(View.VISIBLE);
save.setEnabled(true);
}
}
如果你在代碼閱讀我的意見,你看我想用我的界面實現的方法調用我的MainActivity。問題是,我無法弄清楚如何將我的活動一次發送到我的LoginDialog。
我的意思是,首先它需要上下文,第二我想提供它LoginDialogDismissListener(應該可能改變這個名字......),這是我的活動。
我覺得有一個更好的解決辦法莫過於:
//Constructor
public LoginDialog(Context context, LoginDialogDismissListener listener) {
}
//Activity creating the Dialog
new LoginDialog(this, this);
我試圖定義構造函數只有LoginDialogDismissListener
的說法,但我不能投,或以某種方式獲取情況下,我需要我的super(context)
從其他活動中使用此對話框 – Johannes 2012-08-09 11:07:20