帶有輸入框的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();    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章