0

我有三个片段,Player1Tun、Player2Turn 和 TicTacToeLayout。Player1Turn 和 Player2Turn 每次玩家移动时都会进行内部切换,并且 TicTacToeLayout 包含每次片段切换时更新的井字表。我要做的是将数据从任一播放器片段发送到主机活动(当程序仍在运行时),然后从那里更新 TicTacToeLayout 中的表。有什么想法或方法我可以做到吗?

玩家1:

package As2.packageTK;

//import android.app.Activity;
import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class PlayerTurn2 extends Fragment{

    TextView p2Name;
    TextView p2Icon;
    Button doneP2;
    Button resetP2;
    EditText row;
    EditText column;

    TicTacToeLayout myObject2 = new TicTacToeLayout();
    ArrayList<String> player2;
    Bundle extras = new Bundle();

    int turn = 2;



    public View onCreateView(LayoutInflater inflater,
    ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.playerturn2, container, false);
        return view;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        extras = getArguments();

        player2 = new ArrayList<String>(extras.getStringArrayList("player2"));

        //Toast.makeText(getActivity(), player2.get(0), Toast.LENGTH_LONG).show();
        //Toast.makeText(getActivity(), player2.get(1), Toast.LENGTH_LONG).show();

        p2Name = (TextView) getActivity().findViewById(R.id.p2NameInfo);
        p2Icon = (TextView) getActivity().findViewById(R.id.p2IconInfo);
        row = (EditText) getActivity().findViewById(R.id.rowP2);
        column = (EditText) getActivity().findViewById(R.id.columnP2);
        doneP2 = (Button) getActivity().findViewById(R.id.doneP2);
        //resetP2 = (Button) getActivity().findViewById(R.id.resetP2);

        setPlayer(); //sets all the information of player 2, name, icon, image, etc


        doneP2.setOnClickListener(new View.OnClickListener() {          
            public void onClick(View v) {   

                callPlayer1Fragment(); //switches with first player
            }
        });
    }


    public void callPlayer1Fragment()
    {
        FragmentManager fm       = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment Player1Frag = new PlayerTurn1();
        Player1Frag.setArguments(extras);
        ft.replace(R.id.fragment_container, Player1Frag);
        ft.commit();
    }

    public boolean checkField()
    {
        if(row == null || column == null)
        {
            Toast.makeText(getActivity(), "Please input the row or column!", Toast.LENGTH_LONG).show();
            return false;
        }

        else
            return true;
    }

    public void setPlayer()
    {
        String name = player2.get(0);
        if(!name.equals(""))
            p2Name.setText("Player Name: " + name);
        else 
            p2Name.setText("");

        String icon = player2.get(1);

        if(!icon.equals(""))
            p2Icon.setText("Player Icon: " + icon);
        else
            p2Icon.setText("");
    }
}

player 2 片段是完全相同的代码,所以我不会费心添加它。

TicTacToeLayout 片段类:

public class TicTacToeLayout extends Fragment {

    TextView image1, image2, image3, image4, image5, image6, image7, image8, image9;
    TextView[][] images;

    public View onCreateView(LayoutInflater inflater,
    ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tictactoe_layout, container, false);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        image1 = (TextView) getActivity().findViewById(R.id.Image1);
        image2 = (TextView) getActivity().findViewById(R.id.Image2);
        image3 = (TextView) getActivity().findViewById(R.id.Image3);
        image4 = (TextView) getActivity().findViewById(R.id.Image4);
        image5 = (TextView) getActivity().findViewById(R.id.Image5);
        image6 = (TextView) getActivity().findViewById(R.id.Image6);
        image7 = (TextView) getActivity().findViewById(R.id.Image7);
        image8 = (TextView) getActivity().findViewById(R.id.Image8);
        image9 = (TextView) getActivity().findViewById(R.id.Image9);

        images = new TextView[][]{ {image1, image2, image3},
                                   {image4, image5, image6},
                                   {image7, image8, image9} };

        toast();
    }

    public void toast()
    {
        Toast.makeText(getActivity(), images[0][0].getText().toString(), Toast.LENGTH_LONG).show();
        Toast.makeText(getActivity(), images[0][1].getText().toString(), Toast.LENGTH_LONG).show();
        Toast.makeText(getActivity(), images[0][2].getText().toString(), Toast.LENGTH_LONG).show();
        Toast.makeText(getActivity(), images[1][0].getText().toString(), Toast.LENGTH_LONG).show();
    }

    public void play(int row, int column, String icon)
    {
            images[row-1][column-1].setText(icon);
    }

}

这是 playerturn2 的 XML 布局

<?xml version="1.0" encoding="UTF-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow
            android:id="@+id/tablerow0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/player2Num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="30dp"
                android:layout_marginLeft="100dp"
                android:gravity="center_horizontal"
                android:text="Player 2"
                android:textSize="20dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tablerow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/p2NameInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Player Name: "
                android:textSize="20dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tablerow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/p2IconInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Player icon: "
                android:textSize="20dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tablerow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/p2PicInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Player picture:"
                android:textSize="20dp" />

        </TableRow>

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@android:drawable/editbox_background" />

        <TableRow
            android:id="@+id/rowNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/TextView01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="30dp"
                android:layout_marginLeft="50dp"
                android:gravity="center_horizontal"
                android:text="Row (1-3):"
                android:textSize="20dp" />

            <EditText
                android:id="@+id/rowP2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="number"
                android:width="50dp" />
        </TableRow>

        <TableRow
            android:id="@+id/colNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/TextView02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="30dp"
                android:layout_marginLeft="50dp"
                android:gravity="center_horizontal"
                android:text="Col (1-3):"
                android:textSize="20dp" />

            <EditText
                android:id="@+id/columnP2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="number"
                android:width="50dp" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp" >

            <Button
                android:id="@+id/doneP2"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="Done"
                />

            <Button
                android:id="@+id/resetP2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Reset"
                />

        </TableRow>

    </TableLayout>

这是 tictactoelayout.xml 的布局

<?xml version="1.0" encoding="UTF-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tictactoe"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dp" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp" >

            <TextView
                android:id="@+id/Image1"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="1"
                android:textSize="70dp" />

            <TextView
                android:id="@+id/Image2"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="2"
                android:textSize="70dp" />

            <TextView
                android:id="@+id/Image3"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="3"
                android:textSize="70dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/Image4"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="4"
                android:textSize="70dp" />

            <TextView
                android:id="@+id/Image5"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="5"
                android:textSize="70dp" />

            <TextView
                android:id="@+id/Image6"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="6"
                android:textSize="70dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/Image7"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="7"
                android:textSize="70dp" />

            <TextView
                android:id="@+id/Image8"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="8"
                android:textSize="70dp" />

            <TextView
                android:id="@+id/Image9"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="9"
                android:textSize="70dp" />
        </TableRow>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/win"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:text="Winner"
                android:textSize="20dp" />

            <EditText
                android:id="@+id/winner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="15dp"
                android:ems="10" />

        </RelativeLayout>

    </TableLayout>

最后是宿主活动的 xml 布局:tictactoegame.xml

<?xml version="1.0" encoding="UTF-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <FrameLayout
         android:id="@+id/fragment_container"
         android:layout_width="0px"
         android:layout_height="match_parent" 
         android:layout_weight="1" />

        <fragment
            android:id="@+id/frag2"
            android:name="As2.packageTK.TicTacToeLayout"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="1" />

    </LinearLayout>

我的朋友做了同样的事情,但是实现了他在片段中创建的接口并让主要活动实现了接口。有了这个,他使用了一种方法来传递数据。也许不清楚,但如果你知道我想说什么,那么你可以为我们所有人清除它:)

4

1 回答 1

0

您的朋友是对的,文档中很好地解释了与其他片段的通信:

http://developer.android.com/training/basics/fragments/communicating.html

于 2013-03-27T19:42:23.060 回答