0
類GamePlay工作良好,直到我將活動轉換爲片段。爲什麼當我將活動轉換爲片段按鈕不工作
按鈕在佈局中不起作用fragment_game.xml android:onClick="newGame"
不對點擊執行操作。
我不想添加Listner。
非常感謝:-)
public void newGame(View view) {
noughtsTurn = false;
board = new char[3][3];
resetButtons();
}
類遊戲的幫助:
public class GamePlay extends Fragment {
// Representing the game state:
private boolean noughtsTurn = false; // Who's turn is it? false=X true=O
private char board[][] = new char[3][3]; // for now we will represent the board as an array of characters
private LinearLayout lLayout = null;
private FragmentActivity faActivity = null;
/**
* Called when the activity is first created.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
faActivity = (FragmentActivity) super.getActivity();
lLayout = (LinearLayout) inflater.inflate(R.layout.fragment_game, container, false);
setupOnClickListeners();
resetButtons();
return lLayout;
}
/**
* Called when you press new game.
*
* @param view the New Game Button
*/
public void newGame(View view) {
noughtsTurn = false;
board = new char[3][3];
resetButtons();
}
/**
* Reset each button in the grid to be blank and enabled.
*/
private void resetButtons() {
TableLayout T = (TableLayout) lLayout.findViewById(R.id.tableLayout);
for (int y = 0; y < T.getChildCount(); y++) {
if (T.getChildAt(y) instanceof TableRow) {
TableRow R = (TableRow) T.getChildAt(y);
for (int x = 0; x < R.getChildCount(); x++) {
if (R.getChildAt(x) instanceof Button) {
Button B = (Button) R.getChildAt(x);
B.setText("");
B.setEnabled(true);
}
}
}
}
}
fragment_game.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title"
android:id="@+id/titleText"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Game"
android:id="@+id/newGameBtn"
android:layout_column="0"
android:layout_gravity="center_horizontal"
android:onClick="newGame"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/tableLayout">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="O"
android:textSize="100dp"
android:id="@+id/topLeft" android:layout_column="1" android:width="100dp" android:height="150dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:textSize="100dp"
android:id="@+id/button" android:layout_column="2" android:width="100dp" android:height="150dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:textSize="100dp"
android:id="@+id/button1" android:layout_column="3" android:width="100dp" android:height="150dp"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:textSize="100dp"
android:id="@+id/button2" android:layout_column="1" android:width="100dp" android:height="150dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:textSize="100dp"
android:id="@+id/button3" android:layout_column="2" android:width="100dp" android:height="150dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:textSize="100dp"
android:id="@+id/button4" android:layout_column="3" android:width="100dp" android:height="150dp"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:textSize="100dp"
android:id="@+id/button5" android:layout_column="1" android:width="100dp" android:height="150dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:textSize="100dp"
android:id="@+id/button6" android:layout_column="2" android:width="100dp" android:height="150dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
android:textSize="100dp"
android:id="@+id/button7" android:layout_column="3" android:width="100dp" android:height="150dp"/>
</TableRow>
</TableLayout>
</LinearLayout>