我需要将varible值从对话框传递给activity
我的对话框如下所示:
public class dataa extends Dialog {
public dataa(Context context) {
super(context);
}
EditText txtPASS;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.data);
setTitle(" TEST ");
Button btnOK;
Button btnexit;
txtName = (TextView)findViewById(R.id.txtName);
txtName.setText("TEST");
txtPASS = (EditText)findViewById(R.id.txtPass);
btnOK = (Button) findViewById(R.id.btnOK);
btnOK.setOnClickListener(new Close());
btnexit = (Button) findViewById(R.id.btnExit);
btnexit.setOnClickListener(new exit());
}
private class Close implements android.view.View.OnClickListener {
public void onClick(View v) {
MyParam.TmpPass = txtPASS.getText().toString().trim(); --> this the value i need to use in my activity
dataa.this.dismiss();
}
}
private class exit implements android.view.View.OnClickListener {
public void onClick(View v) {
MyParam.TmpPass = txtPASS.getText().toString().trim(); --> this the value i need to use in my activity
dataa.this.dismiss();
}
}
}
我将EditText txtPASS
的值转移到我班级的MyParam.TmpPass
按下按钮我打开对话框,需要对val进行操作
我的活动如下:
public void Delete_Record()
{
final dataa myDialog = new dataa(context);
myDialog.show();
Button btnOK=(Button)myDialog.findViewById(R.id.btnOK);
btnOK.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (MyParam.TmpPass.equals(MyParam.SYSPASS)) <-- i cant get the value of MyParam.TmpPass from the dialog
{
//if the password good make some thing
myDialog.cancel();
}
else
{
return;
}
}
});
Button btnExit=(Button)myDialog.findViewById(R.id.btnExit);
btnExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myDialog.cancel();
}
});
}
答案 0 :(得分:0)
你目前走错了路。接口是实现这一目标的最佳方式。
我已经编辑了你的代码试试这个并告诉我。
<强> dataa.java 强>
public class dataa extends Dialog {
public dataa(Context context,MyDialogListener myDialogListener) {
super(context);
this.myDialogListener=myDialogListener;
}
MyDialogListener myDialogListener;
EditText txtPASS;
public interface MyDialogListener{
void onOk(dataa dialog,String pass);
void onExit(dataa dialog);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.data);
setTitle(" TEST ");
Button btnOK;
Button btnexit;
txtName = (TextView)findViewById(R.id.txtName);
txtName.setText("TEST");
txtPASS = (EditText)findViewById(R.id.txtPass);
btnOK = (Button) findViewById(R.id.btnOK);
btnOK.setOnClickListener(new Close());
btnexit = (Button) findViewById(R.id.btnExit);
btnexit.setOnClickListener(new exit());
}
private class Close implements android.view.View.OnClickListener {
public void onClick(View v) {
//MyParam.TmpPass = txtPASS.getText().toString().trim(); --> this the value i need to use in my activity
//dataa.this.dismiss();
myDialogListener.onOk(dataa.this,txtPASS.getText().toString().trim());
}
}
private class exit implements android.view.View.OnClickListener {
public void onClick(View v) {
//MyParam.TmpPass = txtPASS.getText().toString().trim(); --> this the value i need to use in my activity
myDialogListener.onExit(dataa.this);
dataa.this.dismiss();
}
}
}
如何从活动中访问?
public void Delete_Record()
{
final dataa myDialog = new dataa(this,new dataa.MyDialogListener() {
@Override
public void onOk(dataa dialog, String pass) {
if (pass.equals(MyParam.SYSPASS))
{
//if the password good make some thing
dialog.cancel();
}
}
@Override
public void onExit(dataa dialog) {
}
});
myDialog.show();
}