如何从Alertdialog.builder获取editText数据?

时间:2011-03-02 06:00:26

标签: android

package com.nil.in;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class SettingsMod extends Activity {
    /** Called when the activity is first created. */
    private ListView listview;
    private String lv_arr[] = { "change password", "change contact details" };
    String old_password = "rajesh";
// EditText input;
// TextView name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.homesettings);

    listview = (ListView) findViewById(R.id.View1_homesettings);
    listview.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, lv_arr));
    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,
                long arg3) {
            // get the position where the user clicked on the ListView
            if (position == 0) {
                AlertDialog.Builder alertbox = new AlertDialog.Builder(
                        SettingsMod.this);
                alertbox.setMessage("Password change!");

                alertbox.setView(LayoutInflater.from(SettingsMod.this)
                        .inflate(R.layout.passwordchange, null));
                alertbox.setPositiveButton("Save",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0,
                                    int arg1) {
                                System.out.println("Save clicked");
                                // getting the editText data from the Dailog
                                Context mContext = getApplicationContext();
                                LayoutInflater inflater = (LayoutInflater) mContext
                                        .getSystemService(LAYOUT_INFLATER_SERVICE);
                                View layout = inflater
                                        .inflate(
                                                R.layout.passwordchange,
                                                (ViewGroup) findViewById(R.id.LinearLayout01));

                                EditText input = (EditText) layout
                                        .findViewById(R.id.Oldpassword_Edit1);
                                EditText newPass_Edit = (EditText) layout
                                        .findViewById(R.id.NewPassword_Edit2);
                                EditText confirm_Edit = (EditText) layout
                                        .findViewById(R.id.Confirm_Edit3);
                                String str = input.getText().toString();
                                String edit_2 = newPass_Edit.getText()
                                        .toString();
                                String edit_3 = newPass_Edit.getText()
                                        .toString();
                                validate(str, edit_2, edit_3);
                            }
                        });
                alertbox.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0,
                                    int arg1) {
                                System.out.println("Cancel clicked");

                            }
                        });

                alertbox.show();

            } else {
                AlertDialog.Builder alertbox = new AlertDialog.Builder(
                        SettingsMod.this);
                alertbox.setTitle("Contact info");
                alertbox.setView(LayoutInflater.from(SettingsMod.this)
                        .inflate(R.layout.detailschange, null));
                alertbox.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0,
                                    int arg1) {

                            }
                        });
                alertbox.show();
            }

        }

    });

}

void validate(String str, String edit_2, String edit_3) {
    if (str == null || str.length() == 0) {
        AlertDialog.Builder adb = new AlertDialog.Builder(SettingsMod.this);
        adb.setTitle("enter old password");
        adb.setPositiveButton("ok", null);
        adb.show();
    }
    if (edit_2 == null || edit_2.length() == 0) {
        AlertDialog.Builder adb = new AlertDialog.Builder(SettingsMod.this);
        adb.setTitle("enter new password");
        adb.setPositiveButton("ok", null);
        adb.show();
    }
    if (!edit_2.equals(edit_3)) {
        AlertDialog.Builder adb = new AlertDialog.Builder(SettingsMod.this);
        adb.setTitle("matched");
        adb.setPositiveButton("ok", null);
        adb.show();
    }

}

}

1 个答案:

答案 0 :(得分:0)

@Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,
                long arg3) {
            // get the position where the user clicked on the ListView
            if (position == 0) {
                AlertDialog.Builder alertbox = new AlertDialog.Builder(
                        SettingsMod.this);
                alertbox.setMessage("Password change!");

 Context mContext = getApplicationContext();
                                LayoutInflater inflater = (LayoutInflater) mContext
                                        .getSystemService(LAYOUT_INFLATER_SERVICE);
                                View layout = inflater
                                        .inflate(
                                                R.layout.passwordchange,
                                                (ViewGroup)                  findViewById(R.id.LinearLayout01));

                                EditText input = (EditText) layout
                                        .findViewById(R.id.Oldpassword_Edit1);
                                EditText newPass_Edit = (EditText) layout
                                        .findViewById(R.id.NewPassword_Edit2);
                                EditText confirm_Edit = (EditText) layout
                                        .findViewById(R.id.Confirm_Edit3);

                alertbox.setView(LayoutInflater.from(SettingsMod.this)
                        .inflate(R.layout.passwordchange, null));

                alertbox.setPositiveButton("Save",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0,
                                    int arg1) {
                                System.out.println("Save clicked");
                                // getting the editText data from the Dailog

                                String str = input.getText().toString();
                                String edit_2 = newPass_Edit.getText()
                                        .toString();
                                String edit_3 = newPass_Edit.getText()
                                        .toString();
                                validate(str, edit_2, edit_3);
                            }
                        });
                alertbox.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0,
                                    int arg1) {
                                System.out.println("Cancel clicked");

                            }
                        });

                alertbox.show();

            }