Categories
Android 🏴‍☠️

Intend & Bundle

สลับ 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();	
        }
    };
}

Categories
Android 🏴‍☠️

Hello Android


Coding ในส่วนของ helloandroid.java ครับ
  

package com.example.helloandroid;

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

public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

       Button okButton = (Button) findViewById(R.id.ok);
       okButton.setOnClickListener(okOnClickListener);
       Button cancelButton = (Button) findViewById(R.id.cancel);
       cancelButton.setOnClickListener(cancelOnClickListener);
}
   private Button.OnClickListener okOnClickListener =
             new Button.OnClickListener(){
       @Override
       public void onClick(View v) {
           EditText edit_text = (EditText) findViewById(R.id.message_text);
           CharSequence edit_text_value = edit_text.getText();
           setTitle("Hello:"+edit_text_value);
       }
   };

   private Button.OnClickListener cancelOnClickListener =
           new Button.OnClickListener(){
       @Override
       public void onClick(View v) {
           finish();
       }
   };
}