สลับ activity ของ 2 layout โดยใฃ้ intend

TestOnClick.java


package test.android.TestOnClick;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TestOnClick extends Activity {
    /** Called when the activity is first created. */
	Button btGotoMain2 ;
	Button btCloseMain1 ;
	EditText edText01 ;
	EditText edText02 ;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMain1();
    }
    
    private void setMain1(){
        setContentView(R.layout.main);
        
        btGotoMain2 = (Button) findViewById(R.id.BtGotoMain2);
        btGotoMain2.setOnClickListener(goMain2ClickListener);
        
        btCloseMain1 = (Button) findViewById(R.id.BtCloseMain1);
        btCloseMain1.setOnClickListener(closeMain1ClickListener);   	
    };
    
    
    private Button.OnClickListener goMain2ClickListener = new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
        	edText01 = (EditText) findViewById(R.id.EdText01);
        	CharSequence edText01_value = edText01.getText();

        	edText02 = (EditText) findViewById(R.id.EdText02);
        	CharSequence edText02_value = edText02.getText();
        	
            Intent intent = new Intent();
            intent.setClass(TestOnClick.this, TestMain2.class);
            
            Bundle bundle = new Bundle();
            bundle.putCharSequence("bText01", edText01_value);
            bundle.putCharSequence("bText02", edText02_value);

            intent.putExtras(bundle);
            
            startActivity(intent);
            finish();       	
        }
    };    
       
    private Button.OnClickListener closeMain1ClickListener = new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
        finish();	
        }
    };
}

TestMain2.java


package test.android.TestOnClick;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TestMain2 extends Activity {
    /** Called when the activity is first created. */
	Button btGotoMain1 ;
	Button btCloseMain2 ;	
	TextView tvText01 ;
	TextView tvText02 ;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMain2();
        
        Bundle bundle = this.getIntent().getExtras();
        CharSequence text01_value = bundle.getCharSequence("bText01");
        tvText01 = (TextView) findViewById(R.id.TextView01);
        tvText01.setText("Text01 : "+text01_value);
        
        CharSequence text02_value = bundle.getCharSequence("bText02");
        tvText02 = (TextView) findViewById(R.id.TextView02);
        tvText02.setText("Text02 : "+text02_value);
    }
    
    private void setMain2(){
        setContentView(R.layout.main2);
        
        btGotoMain1 = (Button) findViewById(R.id.BtGotoMain1);
        btGotoMain1.setOnClickListener(goMain1ClickListener);
        
        btCloseMain2 = (Button) findViewById(R.id.BtCloseMain2);
        btCloseMain2.setOnClickListener(closeMain2ClickListener);   	
    };
    
    private Button.OnClickListener goMain1ClickListener = new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(TestMain2.this, TestOnClick.class);
            
            startActivity(intent);
            finish();              	
        }
    };
    
    private Button.OnClickListener closeMain2ClickListener = new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
        finish();	
        }
    };
}


androidmanifast.xml




    
        
            
                
                
            
        
        
    
    




Intend & Bundle

Comments

comments

Tagged on: