将数据从对话框传递到创建的活动

时间:2012-02-25 20:44:16

标签: android dialog

我正在尝试从创建此对话框的活动的对话框中获取一个字符串,两个整数和一个timepicker结果。如何将此信息传递给此活动? 下面是我的代码。

package ilachatirlatici.pack.net;

 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;

public class HapHatirlatici extends Activity{

Button ekleButton;
boolean eklendiMi;
EditText ilacAdi;
String ilacAdiString;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.haphatirlatici);

    // After creating the activity setting other things for running
    ekleButton = (Button) findViewById(R.id.EkleButton);
    eklendiMi = false;
    ilacAdiString = "";
    ekleButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub
        ilacAdi = (EditText) findViewById(R.id.KayitAdiEditText);
        View layout = getLayoutInflater().inflate(R.layout.ekle_dialog, null);                               
        AlertDialog.Builder builder = new AlertDialog.Builder(HapHatirlatici.this);
        builder.setPositiveButton(R.string.ekle,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                eklendiMi = true;
                ilacAdiString =  ilacAdi.getText().toString();
                //ilacAdiString = ilacAdi.getEditableText().toString();
                databaseEkle(eklendiMi, ilacAdiString);
           }
        });
        builder.setNegativeButton(R.string.vazgec, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                eklendiMi = false;
                databaseEkle(eklendiMi, "");
           }
        });
        builder.setView(layout);
        AlertDialog alertDialog = builder.create();
        alertDialog.setTitle("Ilac Ekleme");
        alertDialog.show();


        }
    });
}
public boolean databaseEkle(boolean eklemeVarMi, String ilacAdi)
{
    boolean sonuc = false;
    System.out.println(ilacAdi);
    return sonuc;
}

}

我试图将结果放在正面按钮部分。我只实现了字符串部分。由于其他部分会相同。如何将此字符串传递给databaseEkle()函数?

1 个答案:

答案 0 :(得分:1)

您实际上是在尝试访问EditText,它实际上位于AlertDialog的布局 R.layout.ekle_dialog 中。您需要参考您的膨胀布局以引用edittext。

设置按钮的onClick侦听器,如下所示:

ekleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    // TODO Auto-generated method stub

    View layout = getLayoutInflater().inflate(R.layout.ekle_dialog, null);
    EditText myEditText = (EditText) layout.findViewById(R.id.KayitAdiEditText);
    AlertDialog.Builder builder = new AlertDialog.Builder(HapHatirlatici.this);
    builder.setPositiveButton(R.string.ekle,new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            eklendiMi = true;
            ilacAdiString =  myEditText.getText().toString();
            //ilacAdiString = ilacAdi.getEditableText().toString();
            databaseEkle(eklendiMi, ilacAdiString);
       }
    });
    builder.setNegativeButton(R.string.vazgec, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            eklendiMi = false;
            databaseEkle(eklendiMi, "");
       }
    });
    builder.setView(layout);
    AlertDialog alertDialog = builder.create();
    alertDialog.setTitle("Ilac Ekleme");
    alertDialog.show();


    }
});

在此示例中,我使用的是myEditText而不是ilacAdi,因为您已声明您的editText为全局,在这种情况下不需要