【问题标题】:Settings Activity- new Dialog设置活动 - 新对话框
【发布时间】:2013-04-05 09:40:39
【问题描述】:

嗨,谁能告诉我如何制作如下图所示的对话框片段以及如何将结果传递回活动?

【问题讨论】:

  • 先自己试试吧。有足够多的教程来描述该用例。
  • 嘿,网上有教程很容易..

标签: android dialog


【解决方案1】:
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();

【讨论】:

    【解决方案2】:

    使用一个编辑文本和两个按钮创建一个自定义对话框。

    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();
    
       }
    

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多