Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Android Tutorials - สอนเขียน Android App ฟรี เขียนโปรแกรมแอนดรอยด์บน SmartPhone / Tablets > Android กับ Intent ควบคุมการแสดงและซ่อน (Show/Hide) ของ Activity form ต่าง ๆ



Clound SSD Virtual Server

Android กับ Intent ควบคุมการแสดงและซ่อน (Show/Hide) ของ Activity form ต่าง ๆ

Android กับ Intent : ซ่อนแสดง (Show/Hide) Activity ในการเขียน Application บน Android นั้นการจัดรูปแบบและวาง Process ของ Layout สามารถควบคุมการแสดงผลในแต่ล่ะ Layout ได้หลายวิธี และสามารถทำได้ใน Activity (Form) เดียวกัน เช่น การใช้ ViewAnimator ที่สามารถควบคุมการแสดงผลได้ในแต่ล่ะ Layout หรือจะใช้ ViewFlipper และ ViewSwitcher ซึ่ง Widgets เหล่านี้สามารถควบคุมการแสดงผลได้ในแต่ล่ะ Layout เช่นเดียวกัน สามารถเข้าไปอ่านในตัวอย่างตามลิ้งค์นั้น ๆ

แต่ถ้าต้องการแยกการทำงานของแต่ล่ะ Activity Form ออกจากกัน เพื่อง่ายและอีสระต่อสะดวกในการเขียนโปรแกรมก็สามารถทำได้ง่าย ๆ เช่นเดียวกัน โดยใน Android มี class ที่ชื่อว่า Intent ที่จะเข้ามาจัดการในการแสดงผลของ Activity ต่าง ๆ

Flow การทำงาน

Android Intent (Show/Hide) Activity


จากภาพประกิบจะเห็นว่ามี Activity อยู่ 3 Form ในที่นี้เรามองว่าเป็น MainActivity.class , ActivityForm2.class และ ActivityForm3.class ซึ่งในแต่ล่ะ Class ทำงานแยก Form และ เรียกใช้ XML คนล่ะตัวกัน ในกรณีที่ต้องการย้ายการทำงานระว่าง Activity นั้น สามารถใช้ startActivity() ได้ตามตัวอย่าง

Example 1. ย้ายจาก MainActivity.class ไปยัง ActivityForm2.class
            	Intent newActivity = new Intent(MainActivity.this,ActivityForm2.class);
            	startActivity(newActivity);


Example 2. ย้ายจาก ActivityForm2.this ไปยัง ActivityForm3.class
            	Intent newActivity = new Intent(ActivityForm2.this,ActivityForm3.class);
            	startActivity(newActivity);

และในกรณีเดียวกันก็สามารถย้ายกลับมายัง Activity หลักได้เช่นเดียวกัน

Example 3. ย้ายจาก ActivityForm3.this ไปยัง MainActivity.class
            	Intent newActivity = new Intent(ActivityForm3.this,MainActivity.class);
            	startActivity(newActivity);


การส่งค่าตัวแปร Parameters ระหว่าง Activity

นอกจากนี้ในการย้ายการทำงานระหว่าง Activity ก็สามารถส่งค่า Parameters หรือตัวแปรไปกับ Intent ได้เช่นเดียวกัน
	            	Intent newActivity = new Intent(MainActivity.this,ActivityForm2.class);
	            	newActivity.putExtra("MemID", "1234");
	            	startActivity(newActivity);

การอ่านค่าตัวแปรจาก Intent ใน Activity ที่เป็น Form รับ
		Intent intent= getIntent();
		final String MemID = intent.getStringExtra("MemID"); // for String
		int MemID = intent.getExtras().getInt("MemID"); // for Int


กรณีส่งมากกว่า 1 ค่า Parameters
	            	Intent newActivity = new Intent(MainActivity.this,ActivityForm2.class);
	            	newActivity.putExtra("MemID", "1234");
	            	newActivity.putExtra("Name", "Win");
	            	startActivity(newActivity);

การอ่านค่าตัวแปรจาก Intent ใน Activity ที่เป็น Form รับ
		Intent intent= getIntent();
		final String MemID = intent.getStringExtra("MemID"); 
		final String Name = intent.getStringExtra("Name"); 


จะเห็นว่าการย้ายการทำงานระหว่าง Activity นั้นสามารถทำได้อย่างไม่ยาก แต่ทั้งนี้การที่จะออกแบบหลาย Activity นั้นก็ควรแยกเฉพาะ Process ที่ทำงานแยกกันอย่างสิ้นเชิง เพราะใน Android มี Widgets ต่าง ๆ ที่สามารถควบคุมการทำงานของแต่ล่ะ Layout ได้อยู่แล้ว อาจจะใช้ TabHost เข้ามาจัดการก็ได้เช่นเดียวกัน

Example ตัวอย่างการใช้ Intent กับ Activity
หลังจากที่ได้รู้ว่า Intent มีหลักการทำงานในขั้นพื้นฐานแล้ว เรามาลองเขียนการ Intent Activity แบบง่าย ๆ ซะ 2-3 Form

โครงสร้างเริ่มต้น

Android Intent (Show/Hide) Activity

ในโครงสร้างเริ่มต้นเรามีไฟล์เพียง 2 ตัวคือ MainActivity.java กับ activity_main.xml เป็น Activity หลัก ซึ่งเป็นไฟล์ที่ทำงานคู่กัน

เราจะลองเพิ่ม Activity และ XML layout ขึ้นมาอีก 2 ตัวคือ
- ActivityForm2.java กับ activity_form2.xml
- ActivityForm3.java กับ activity_form3.xml








การเพิ่ม XML Layout
คลิกขวาในตำแหน่งของ Layout ที่ต้องการ

Android Intent (Show/Hide) Activity

เลือก New -> Android XML Form

Android Intent (Show/Hide) Activity

กรอกชื่อ XML Layout

การเพิ่ม Java Class
คลิกขวาในตำแหน่งของ package ที่ต้องการ

Android Intent (Show/Hide) Activity

เลือก New -> Class

Android Intent (Show/Hide) Activity

กรอกชื่อ Class

ทดสอบเพิ่มให้ครบทั้ง 3 ตัว

Android Intent (Show/Hide) Activity

เราจะได้ไฟล์ทั้งหมด 3 คู่ด้วยกัน ดังภาพประกอบ

เพิ่มเติม
อย่าลืม !!! หลังจากที่เพิ่ม Class ทุกครั้งอย่าลืมประกาศค่าในไฟล์ AndroidManifest.xml ทุกครั้งไม่เช่นนั้นจะไม่สามารถรันโปรแกรมได้

รูปแบบ
<activity 
    android:name="ActivityForm3" />


Android Intent (Show/Hide) Activity

AndroidManifest.xml
        <activity 
            android:name="ActivityForm2"
            android:theme="@style/AppTheme"
            android:screenOrientation="portrait"            
            android:label="@string/title_activity_main" />
        
        <activity 
            android:name="ActivityForm3"
            android:theme="@style/AppTheme"
            android:screenOrientation="portrait"            
            android:label="@string/title_activity_main" />   

เพิ่มในตำแหน่งนี้
Android Intent (Show/Hide) Activity



รายละเอียดของไฟล์ทั้ง 3 คู่

- activity_main.xml กับ MainActivity.java

activity_main.xml
<RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Current Show Form 1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="62dp"
        android:text="Show Form 2" />

</RelativeLayout>

Android Intent (Show/Hide) Activity

MainActivity.java
package com.myapp;

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

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	
		// Button1
        final Button btn1 = (Button) findViewById(R.id.button1);
        // Perform action on click
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	// Open Form 2
            	Intent newActivity = new Intent(MainActivity.this,ActivityForm2.class);
            	startActivity(newActivity);
        
            }
        });
        
	}

}









- activity_form2.xml กับ ActivityForm2.java

activity_form2.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Current Show Form 2"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
     <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="62dp"
        android:text="Show Form 3" />

</RelativeLayout>

Android Intent (Show/Hide) Activity

ActivityForm2.java
package com.myapp;

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

public class ActivityForm2 extends Activity  {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_form2);
		
		// Button1
        final Button btn1 = (Button) findViewById(R.id.button1);
        // Perform action on click
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	// Open Form 3
            	Intent newActivity = new Intent(ActivityForm2.this,ActivityForm3.class);
            	startActivity(newActivity);
        
            }
        });
	
	}
    
}


- activity_form3.xml กับ ActivityForm3.java

activity_form3.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Current Show Form 3"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
   	<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="62dp"
        android:text="Back to Form 1" />

</RelativeLayout>

Android Intent (Show/Hide) Activity

ActivityForm3.java
package com.myapp;

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

public class ActivityForm3 extends Activity  {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_form3);
		
		// Button1
        final Button btn1 = (Button) findViewById(R.id.button1);
        // Perform action on click
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	// Back to Form 1
            	Intent newActivity = new Intent(ActivityForm3.this,MainActivity.class);
            	startActivity(newActivity);
        
            }
        });
	
	}
    
}


Screenshot

Android Intent (Show/Hide) Activity

จาก Main Activity จะ Intent ไปยัง Activity Form 2

Android Intent (Show/Hide) Activity

จาก Activity Form 2 จะ Intent ไปยัง Activity Form 3

Android Intent (Show/Hide) Activity

จาก Activity Form 3 จะ Intent ไปยัง Main Activity

สำหรับการใช้งาน Intent Activity ร่วมกับ From และการส่งค่าจากการใช้จริง สามารถดูได้จากตัวอย่างนี้



Android Intent (Show/Hide) Activity



   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-07-22 07:20:22 / 2017-03-26 20:26:36
  Download : No files
 Sponsored Links / Related

 
เรียกใช้งาน string.xml บน android ไฟล์จัดเก็บค่า string / array ตัวแปรในรูปแบบต่าง ๆ
Rating :

 
Android การใส่ Effect ให้กับ Intent ใน Activity ระหว่างการเปลี่ยน Activity
Rating :

 
Android กับ setContentView() ควบคุมการแสดงผล XML Layout ของแต่ล่ะ Activity
Rating :

 
Android Rotate Switching Portrait and Landscape - Emulator (มุมมองในแนวตั้งและแนวนอน)
Rating :

 
Android Enabled/Disabled Fix Portrait or Landscape Orientation Mode
Rating :

 
Android Change Activity Layout when Switching Portrait and Landscape
Rating :

 
Android Display FullScreen / No Title Bar
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่