带有输入框的AlertDialog

带有输入框的AlertDialog

package com.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.EditText;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class ButtonTest extends Activity {
    private static final String tag = null;
private final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
    private final int FILL_PARENT = ViewGroup.LayoutParams.FILL_PARENT;
    private TextView tv;
    private EditText edit;
    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        tv = new TextView(this);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(linearLayout);
        final Button button = new Button(this);
        button.setText("Open Dialog");
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                edit = new EditText(ButtonTest.this);
                edit.setGravity(Gravity.CENTER);
                final   FrameLayout fl = new FrameLayout(null);
                fl.addView(edit,new FrameLayout.LayoutParams(FILL_PARENT, WRAP_CONTENT)); //给某屏幕添加组件
                edit.setText("Preset Text");
                AlertDialog alerdialog=dialogCreate();
                alerdialog.setView(fl);//dialog接收(设置)该组件
                alerdialog.show();//EditText 就可以显示在对话框中了
            }
        });
        linearLayout.addView(button, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
        linearLayout.addView(tv, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
    }
       
    public AlertDialog dialogCreate(){
     return new AlertDialog.Builder(ButtonTest.this) 
    
     .setTitle("input the file name:")               
     .setPositiveButton("ok", new DialogInterface.OnClickListener() {                   
      public void onClick(DialogInterface dialog, int whichButton) {                       
       /* User clicked OK so do some stuff */
             tv.setText(edit.getText());
             Log.v(tag, "the content is=" + tv.getText().toString());
       }               
      })               
     .setNegativeButton("cacle", new DialogInterface.OnClickListener() {                   
      public void onClick(DialogInterface dialog, int whichButton) {                       
       /* User clicked Cancel so do some stuff */                   
       }               
      })               
     .create();    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值