เนื่องจากใน android studio version ใหม่จะใช้ fragment layout มาช่วยในส่วนของ Activity ทำให้การเขียนโค้ดในปุ่ม button เพื่อเปลี่ยนหน้าหรือเปลี่ยน activity ต้องย้ายไปทำในส่วนของ fragment แทน

 /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            final Button btnFlag=(Button) rootView.findViewById(R.id.buttonflagment);
            btnFlag.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent newActivityReminder=new Intent();
                    newActivityReminder.setClass(getActivity(),MainActivityReminder.class);
                    newActivityReminder.putExtra("index","test");//ส่งค่าตัวหลังเป็น Value

                    startActivity(newActivityReminder);
                }
            });

            return rootView;
        }

    }

อันนี้โค้ดทั้งหมดในหน้า MainActivity

package com.medicinereminder;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        /*
        final AlertDialog.Builder adb = new AlertDialog.Builder(this);

        final Button btn1 = (Button) findViewById(R.id.buttonnew);
        // Perform action on click
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog ad = adb.create();
                ad.setMessage("Button 2 Event");
                ad.show();
            }
        });
        */

        // Button1
        final Button btn1 = (Button) findViewById(R.id.buttonnew);
        // Button2 open Reminder
        final ImageButton btnReminder=(ImageButton)findViewById(R.id.buttonimgreminder);

        // Perform action on click
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                // Open Form 2
                Intent newActivity = new Intent(MainActivity.this,MainActivity2.class);
                startActivity(newActivity);

            }
        });

        btnReminder.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                //open Reminder
                Intent newActivityReminder=new Intent(MainActivity.this,MainActivityReminder.class);
                startActivity(newActivityReminder);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            final Button btnFlag=(Button) rootView.findViewById(R.id.buttonflagment);
            btnFlag.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent newActivityReminder=new Intent();
                    newActivityReminder.setClass(getActivity(),MainActivityReminder.class);
                    newActivityReminder.putExtra("index","test");//ส่งค่าตัวหลังเป็น Value

                    startActivity(newActivityReminder);
                }
            });

            return rootView;
        }

    }

}

อันนี้หน้า activity_main.xml มีสร้างปุ่ม button ธรรมดาเพื่อให้เห็นความแตกต่างด้วย แต่ถ้าทำจริงไม่ควรสร้างอะไรในไฟล์นี้ไปสร้างใน  fragment แทนดีกว่า

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:background="#CCFFFF">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="27dp"
            android:text="Welcome to Medicine Reminder"
            android:id="@+id/textTitle"
            android:layout_gravity="center_horizontal|top"
            android:layout_margin="10dp"
            android:textAlignment="center"
            android:textSize="@dimen/abc_action_bar_subtitle_text_size"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="130dp"
        android:layout_height="400dp"
        android:layout_gravity="center"
        android:background="#ffffff">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/buttonnew"
            android:layout_gravity="center_horizontal|top"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="51dp"
            android:text="New Text"
            android:id="@+id/textView"
            android:layout_gravity="center_horizontal|top" />

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="130dp"
        android:layout_height="400dp"
        android:layout_gravity="right|center_vertical"
        android:background="#ffffff">

        </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to NFC Update"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal|bottom" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="130dp"
        android:layout_height="400dp"
        android:layout_gravity="left|top"
        android:weightSum="1"
        android:background="#ffffff">

        <ImageButton
            android:layout_width="91dp"
            android:layout_height="wrap_content"
            android:id="@+id/buttonimgreminder"
            android:layout_gravity="center_horizontal"
            android:adjustViewBounds="false"
            android:background="@drawable/main_reminder"
            android:layout_weight="0.18" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="27dp"
            android:text="New Text"
            android:id="@+id/textView2"
            android:layout_gravity="center_horizontal|top" />
    </LinearLayout>

</FrameLayout>

หน้า fragment_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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button on Flagment"
        android:id="@+id/buttonflagment"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:singleLine="false" />
</RelativeLayout>

 

 

 

 

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.