Intent 用法之Activity启动

FirstActivity.java

package com.intent.test;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class IntentActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.main);
        
        Dialog dialog = new Dialog(this);
        LinearLayout layout = new LinearLayout(this);
        
        Button button = new Button(this);
        button.setWidth(240);
        button.setHeight(100);
        button.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent first = new Intent();
				first.setClass(getApplicationContext(), IntentSed.class);
				first.putExtra("Name", "Vicky");
				first.putExtra("STU", false);
				startActivity(first);
				
			}
        	
        });
        layout.addView(button);
        dialog.setTitle("TEST test and test");
        dialog.setContentView(layout);
        dialog.show();
    }
}



IntentSed.java

package com.intent.test;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class IntentSed extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
                        
        Dialog dialog = new Dialog(this);
        // dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);  title is not needed here ;
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        
        final String temp1 = getIntent().getStringExtra("Name");
         final Boolean temp2 = getIntent().getBooleanExtra("STU", true);
        
        Button button = new Button(this);
        button.setWidth(240);
        button.setHeight(100);
        button.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                
//                 Intent second = new Intent();
//                second.setClass(getApplicationContext(), IntentThird.class);
                // or :
                Intent second = new Intent(getApplicationContext() , IntentThird.class);
                
                second.putExtra("Name", temp1);
                second.putExtra("STU", temp2);
                startActivity(second);
                
            }
            
        });       
        layout.addView(button);
        
        TextView text = new TextView(this);
        text.setWidth(240);
        text.setHeight(100);
        text.setText(temp1 + temp2);
        layout.addView(text);
        
        dialog.setTitle("Get Value from A : \n" + temp1 + temp2);
      
        dialog.setContentView(layout);
        dialog.show();              
        
    }
}



IntentThird.java

package com.intent.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class IntentThird extends Activity {
    /** Called when the activity is first created. */
    private AlertDialog myDialog ;
    private AlertDialog PosiDialog ;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        String value1 = getIntent().getStringExtra("Name");
        Boolean value2 = getIntent().getBooleanExtra("STU",true);
               
        
        builder.setItems(new CharSequence[] {value1.toString(),value2.toString()
        }, new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();                
                switch( which ){
                case 0:
                    AlertDialog.Builder mybuilder = new AlertDialog.Builder(IntentThird.this);
                    mybuilder.setMessage("Hello World!");
//                    mybuilder.setNegativeButton("I konw", new OnClickListener(){
//                    
//                    });
                    mybuilder.setPositiveButton(R.string.app_name, new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            PosiDialog.dismiss();
                        }
                        
                    });
                    
                    mybuilder.setOnCancelListener(new DialogInterface.OnCancelListener(){

                        @Override
                        public void onCancel(DialogInterface dialog) {
                            // TODO Auto-generated method stub
                            if(null != PosiDialog){
                                PosiDialog.dismiss();
                            }
                        }
                        
                    });
                    PosiDialog = mybuilder.create();
                    PosiDialog.show();
                    break;
                case 1:
                    
                    break;
                default :
                    ;
                        
                }
                    
            }
        });
        
        myDialog = builder.create();
        myDialog.setTitle("Test App Finish");
      
        //Window window = myDialog.getWindow();
        //WindowManager.LayoutParams Param = window.getAttributes();
       
        //window.setAttributes(Param);
        myDialog.show();
        
    }
    
    @Override
    protected void onDestroy()
    {        
        if(null != myDialog) myDialog.dismiss();
       if(null != PosiDialog) PosiDialog.dismiss();
        super.onDestroy();
    }
}



小结: 利用Intent进行Activities之间切换,并且设计部分dialog使用;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值