下面是button的OnClick代码很简单
dlg= new BaseSettingDlg(this,android.R.style.Theme_NoTitleBar_Fullscreen);
dlg.show();
下面是我自定义的dialog类,
package fac.pro.dialogs;
import fac.pro.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
public class BaseSettingDlg extends Dialog {
public BaseSettingDlg(Context context, int theme) {
super(context, theme);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.root_setting_dlg);
}
}
下面是部局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_margin="10dip"
android:layout_height="wrap_content" android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="其它" android:layout_marginRight="5dip" android:layout_gravity="center_vertical"></TextView>
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="fill_horizontal" android:ems="5" android:singleLine="true" android:numeric="decimal"></EditText>
</LinearLayout>
请问怎么在点按钮弹出dialog后,设置完那个dialog里的EditText里的值后,
在结束dialog弹出询问"是否保存修改",并且在dialog关闭后,
我的主程序能拿到刚才在dialog里修改的editText值. 请直接给代码,别只讲科学道理,没有代码,谢谢!
这个没有什么技术难点吧,
但对于新手来很难搞,请高手能实现需求,能运行的代码,谢谢
------解决思路----------------------
用这个dialog的对象findviewbyid
------解决思路----------------------
package com.example.androiddemos.home;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.widget.EditText;
import com.example.androiddemos.R;
public class CustomerDialog extends Dialog implements OnDismissListener{
private EditText et;
private ContentListener listener;
public CustomerDialog(Context context, int theme, ContentListener listener) {
super(context, theme);
this.listener = listener;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.root_setting_dlg);
et = (EditText) findViewById(R.id.et);
this.setOnDismissListener(this);
}
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
final String content = et.getText().toString();
if(content.isEmpty()) {
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("Do you want save the content : " + content);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
listener.saveContent(content);
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.show();
}
public interface ContentListener {
public void saveContent(String content);
}
}
<?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="10dip"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dip"
android:text="其它" >
</TextView>
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:ems="5"
android:numeric="decimal"
android:singleLine="true" >
</EditText>
</LinearLayout>
/////////////////////////////// mainActivity
package com.example.androiddemos.home;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.androiddemos.R;
import com.example.androiddemos.home.CustomerDialog.ContentListener;
public class DialogDemoActivity extends ActionBarActivity implements ContentListener {
private Button action;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog_demo);
}
public void showDialog(View view) {
Dialog dg = new CustomerDialog(this,android.R.style.Theme_NoTitleBar_Fullscreen, this);
dg.show();
}
@Override
public void saveContent(String content) {
// TODO Auto-generated method stub
Toast.makeText(this, "save " + content, Toast.LENGTH_LONG).show();
}
}