Saturday 15 June 2013

android - Why when I convert Activity to fragment Button not working -



android - Why when I convert Activity to fragment Button not working -

class gameplay work untill convert activity fragment.

the button not working in layout fragment_game.xml android:onclick="newgame" not perform action on click.

i dont want add together listner.

many help:-)

public void newgame(view view) { noughtsturn = false; board = new char[3][3]; resetbuttons(); }

class gameplay:

public class gameplay extends fragment { // representing game state: private boolean noughtsturn = false; // who's turn it? false=x true=o private char board[][] = new char[3][3]; // represent board array of characters private linearlayout llayout = null; private fragmentactivity faactivity = null; /** * called when activity 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(); homecoming llayout; } /** * called when press new game. * * @param view new game button */ public void newgame(view view) { noughtsturn = false; board = new char[3][3]; resetbuttons(); } /** * reset each button in grid blank , enabled. */ private void resetbuttons() { tablelayout t = (tablelayout) llayout.findviewbyid(r.id.tablelayout); (int y = 0; y < t.getchildcount(); y++) { if (t.getchildat(y) instanceof tablerow) { tablerow r = (tablerow) t.getchildat(y); (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>

if don't want manually add together listener, must move onclick method newgame activity fragment attached to.

otherwise, must programmatically set onclicklistener in gameplay (check out this answer clever way of doing this)

android android-fragments onclick

No comments:

Post a Comment