android > AlertDialog.Builder 弹出窗口

 

* 弹出 确认 ,取消 对话框 

 

package broad.com;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BroadActivity extends Activity {
    /** Called when the activity is first created. */
	private Button button_1;
	Builder builder;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        button_1 = (Button)findViewById(R.id.button1);
        button_1.setOnClickListener(new ClickReceive());        
    }    
    class ClickReceive implements OnClickListener{
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		builder = new Builder(BroadActivity.this);
    	      // builder.setIcon(R.drawable.ic_launcher);
    	       builder.setMessage("hello");
    	       builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {   
    	              public void onClick(DialogInterface dialog, int id) {  
    	            	  
    	            	  BroadActivity.this.onCreate(null) ;  //相当于把当前activity 重新刷新
    	              
    	              
    	              }   
    	          });
    	       /*
    	       builder.setTitle("是否删除文件");
    	       builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {   
    	              public void onClick(DialogInterface dialog, int id) {   
    	              BuiderActivity.this.onCreate(null) ;   
    	              }   
    	          });*/
    	       builder.create().show();
    	}
    	////
    }
    
    
}

 

* 弹出文本输入框 ,并将输入结果显示在 TextView

 

package test.mft;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
	private Button b1;
	private TextView v1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1 = (Button)findViewById(R.id.b1);
        v1 = (TextView)findViewById(R.id.v1);
        
        b1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Builder builder = new Builder(TestActivity.this);
				builder.setTitle("请输入");
				builder.setIcon(android.R.drawable.ic_dialog_info);
				final EditText inputText = new EditText(TestActivity.this);
				builder.setView(inputText);
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {     
                    public void onClick(DialogInterface dialog, int id) {  
                    	Appendable value = inputText.getText();
                        v1.setText(value.toString());
                    }     
                });
				builder.setNegativeButton("取消", null);
				builder.show();
			}
		});
        
    }
}
 

 

* 弹出 单选框

 

package test.mft;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
	private Button b1;
	private TextView v1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1 = (Button)findViewById(R.id.b1);
        v1 = (TextView)findViewById(R.id.v1);
        
        b1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Builder builder = new Builder(TestActivity.this);
				builder.setTitle("请选择");
				builder.setIcon(android.R.drawable.ic_dialog_info);
				builder.setSingleChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, 0,   
						  new DialogInterface.OnClickListener() {                    
				     public void onClick(DialogInterface dialog, int which) {  
				   
				        v1.setText(""+which);
				        dialog.dismiss();  //关闭对话框
				        
				     }  
				  }  
				);  
				builder.show();
			}
		});
        
    }
}
 

 

 

 

 

你可能感兴趣的:(AlertDialog)