StartActivityForResult实现确认对话框

1.

llDelete.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {

				//删除弹出对话框
				showDeleteDialog();
			}
		});


/**
	 * 提示一个确认删除的对话框
	 * */
	public void showDeleteDialog()
	{
		Intent intent = new Intent();
		intent.setClass(ItemLongClickedActivity.this, DialogActivityOfComfirmDelete.class);//提示是否保存的Activity
		startActivityForResult(intent, REQUEST_CODE);
	}


private int REQUEST_CODE = 0; 
	private final int COMFIRM_DELETE = 5;
	private final int COMFIRM_NO_DELETE = 6;
	@Override  
    public void onActivityResult(int requestCode, int resultCode, Intent data)  
    {  
         super.onActivityResult(requestCode, resultCode, data);    
         
         if(requestCode == REQUEST_CODE)
         {
        	 if(resultCode == COMFIRM_DELETE)//保存当前联系人
    	     {
    	    	 System.out.println("执行删除联系人的操作");
    	    	 
    	    	 
    	     }
    	     if(resultCode == COMFIRM_NO_DELETE)//不保存
    	     {
    	    	 System.out.println("不删除联系人");
    	    	 
    	     }
         }
	     
    }

 

 

 

2.

 

/**
 * 提示用户是否确认删除
 * */
public class DialogActivityOfComfirmDelete extends Activity
{

	private final int COMFIRM_DELETE = 5;
	
	private final int COMFIRM_NO_DELETE = 6;
	
	private Button btnOk;
	
	private Button btnCancel;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_activity_of_comfirm_delete);
		
		btnOk = (Button)findViewById(R.id.daocd_ok);
		btnCancel = (Button)findViewById(R.id.daocd_cancel);
		
		
		btnOk.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// 确认删除
				delete();
			}
		});
		
	    btnCancel.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// 取消删除
				noDelete();
			}
		});
	}
	
	
	
	public void delete()
	{
		Bundle bundle = new Bundle();  
	    Intent intent = new Intent();  
	    intent.putExtras(bundle);//用Intent保存对象  
	    setResult(COMFIRM_DELETE, intent);  
	    finish(); 
	}
	
	public void noDelete()
	{
		Bundle bundle = new Bundle();  
	    Intent intent = new Intent();  
	    intent.putExtras(bundle);//用Intent保存对象  
	    setResult(COMFIRM_NO_DELETE, intent);  
	    finish(); 
	}
	
}
 

 

3.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff" >
<!-- 提示用户是否确认删除 -->
     <TextView 
	        android:layout_width="fill_parent"
	        android:layout_height="50dip"
	        android:gravity="center"
	        android:layout_weight="1"
			android:textSize="22dip"
			android:textColor="#ffffff"
	        android:text="温馨提示"
	        android:background="#33b5e5"
	        />
    
	<TextView 
	    android:id="@+id/daocd_tip"
	    android:layout_width="300dip"
	    android:layout_height="wrap_content"
	    android:layout_marginTop="25dip"
	    android:layout_marginBottom="25dip"
		android:gravity="center"
	    android:textSize="20dip"
	    android:singleLine="true"
	    android:textColor="#404040"
	    android:text="确认删除该联系人?"
	    />

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:background="#EDEDED"
        >
        
        <Button 
            android:id="@+id/daocd_ok"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="8dip"
            android:layout_marginBottom="4dip"
            android:textColor="#404040"
            android:textSize="20dip"
            android:gravity="center"
            android:text="是"/>
        
        <Button 
            android:id="@+id/daocd_cancel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="8dip"
            android:layout_marginBottom="4dip"
            android:textColor="#404040"
            android:textSize="20dip"
            android:gravity="center"
            android:text="否"/>
        
    </LinearLayout>

</LinearLayout>
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值