1

我正在尝试显示一个可自定义的对话框。此对话框包含 3 个编辑文本和一个时间选择器。当我按下屏幕上的按钮时,我想显示此对话框。我查看了谷歌教程并尝试编写代码。但是,当我使用充气机中的根布局作为其中按下按钮的布局时,它会在按钮下添加对话框。当我在 dialog_xml 中使用根布局时,该按钮不起作用。该代码的一部分在下面。你有什么想法我怎样才能以正确的方式做到这一点?

    Button ekleButton;
    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);
       ekleButton.setOnClickListener(new View.OnClickListener() {   
       public void onClick(View v) {
            // TODO Auto-generated method stub
            // Dialog icin yerlesimler

        AlertDialog.Builder builder;
        AlertDialog alertDialog;

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

        builder = new AlertDialog.Builder(mContext);
        builder.setView(layout);
        alertDialog = builder.create();

        }
    });

在此之后,我编辑代码并添加两个按钮。我想获得所见活动的警报对话框的结果。我写的代码在下面。

公共类 HapHatirlatici 扩展 Activity{

Button ekleButton;
boolean eklendiMi;
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;
    ekleButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub

        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;
           }
        });
        builder.setNegativeButton(R.string.vazgec, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                eklendiMi = false;
           }
        });
        builder.setView(layout);
        AlertDialog alertDialog = builder.create();
        alertDialog.setTitle("Ilac Ekleme");
        alertDialog.show();


        }
    });
}
public boolean databaseEkle()
{
    boolean sonuc = false;
    return sonuc;
}

 }
4

1 回答 1

1

您忘记了alertDialog.show();实际显示您刚刚构建的对话框。

此外,您的代码可以简化:

Button ekleButton;
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);
   ekleButton.setOnClickListener(new View.OnClickListener() {   
       public void onClick(View v) { 
           View layout = getLayoutInflater().inflate(R.layout.ekle_dialog, null);                               
           AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityClass.this);
           builder.setView(layout);
           AlertDialog alertDialog = builder.create();

           // this is what you forgot:
           alertDialog.show();
       }
    });
}

当然,替换YourActivityClass为您的实际活动名称。

于 2012-02-25T17:33:04.720 回答