设置活动 - 新对话框

时间:2013-04-05 09:40:39

标签: android dialog

您好任何人都可以告诉我如何制作如下图所示的对话框片段以及如何将结果传回活动? enter image description here

2 个答案:

答案 0 :(得分:0)

AlertDialog.Builder editalert = new AlertDialog.Builder(this);

editalert.setTitle("messagetitle");
editalert.setMessage("here is the message");


final EditText input = new EditText(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.FILL_PARENT,
        LinearLayout.LayoutParams.FILL_PARENT);
input.setLayoutParams(lp);
editalert.setView(input);

editalert.setPositiveButton("Send via email", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {


    }
});


editalert.show();

答案 1 :(得分:0)

使用editext和两个按钮创建自定义对话框。

dilog.xml

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/ok"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="20dp"
    android:text="OK" />

<Button
    android:id="@+id/anuluj"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/ok"
    android:layout_alignBottom="@+id/ok"
    android:layout_marginLeft="35dp"
    android:layout_toRightOf="@+id/ok"
    android:text="Anuluj" />

</RelativeLayout>

在您的活动中,将自定义布局设置为对话框

public void showpopup()  
{
final Dialog d = new Dialog(MainActivity.this);
d.setTitle("my title");
d.setContentView(R.layout.dialog);
Button ok = (Button) d.findViewById(R.id.ok);
final EditText ed= (EditText) d.findViewById(R.id.editText1);
ok.setOnClickListener(new OnClickListener()
{

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String s= ed.getText().toString();//get text form editext

    }

});
 Button anuluj = (Button) d.findViewById(R.id.anuluj);

    anuluj.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            d.cancel();

        }

    });
    d.show();

   }