我有一个由按钮代表的3X3游戏板。当我启动我的应用程序时,各个方块出现,但我找不到为什么启动新游戏的按钮没有。我正在提供MainActivity的代码:
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.widget.Button;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
// Declare class variables and initialize the board
String square1,
square2,
square3,
square4,
square5,
square6,
square7,
square8,
square9,
newgame;
// Handle button objects
Button square1_button;
Button square2_button;
Button square3_button;
Button square4_button;
Button square5_button;
Button square6_button;
Button square7_button;
Button square8_button;
Button square9_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Assign button objects to ids
square1_button = (Button)findViewById(R.id.r1c1_button);
square2_button = (Button)findViewById(R.id.r1c2_button);
square3_button = (Button)findViewById(R.id.r1c3_button);
square4_button = (Button)findViewById(R.id.r2c1_button);
square5_button = (Button)findViewById(R.id.r2c2_button);
square6_button = (Button)findViewById(R.id.r2c3_button);
square7_button = (Button)findViewById(R.id.r3c1_button);
square8_button = (Button)findViewById(R.id.r3c2_button);
square9_button = (Button)findViewById(R.id.r3c3_button);
}
void resetBoard()
{
String button1_str = square1_button.getText().toString();
String button2_str = square2_button.getText().toString();
String button3_str = square3_button.getText().toString();
String button4_str = square4_button.getText().toString();
String button5_str = square5_button.getText().toString();
String button6_str = square6_button.getText().toString();
String button7_str = square7_button.getText().toString();
String button8_str = square8_button.getText().toString();
String button9_str = square9_button.getText().toString();
square1 = button1_str;
square2 = button2_str;
square3 = button3_str;
square4 = button4_str;
square5 = button5_str;
square6 = button6_str;
square7 = button7_str;
square8 = button8_str;
square9 = button9_str;
// square1 = "";
// square2 = "";
// square3 = "";
// square4 = "";
// square5 = "";
// square6 = "";
// square7 = "";
// square8 = "";
// square9 = "";
// Mark the first three squares with an X
square1 = "X";
square2 = "X";
square3 = "X";
}
}
我非常感谢你提供任何帮助。另外,前三个方块没有设置为“X”,所以我认为我的问题出现在那行代码之前。
这是我的布局:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:columnOrderPreserved="false"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:rowCount="6"
tools:context="com.jtryon.tictactoe.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/r1c1_button"
android:layout_column="0"
android:layout_row="1"
android:layout_width="100sp"
android:layout_height="100sp" />
<Button
android:id="@+id/r1c2_button"
android:layout_column="0"
android:layout_row="2"
android:layout_width="100sp"
android:layout_height="100sp" />
<Button
android:id="@+id/r1c3_button"
android:layout_column="0"
android:layout_row="3"
android:layout_width="100sp"
android:layout_height="100sp" />
<Button
android:id="@+id/r2c1_button"
android:layout_column="1"
android:layout_row="1"
android:layout_width="100sp"
android:layout_height="100sp" />
<Button
android:id="@+id/r2c2_button"
android:layout_column="1"
android:layout_row="2"
android:layout_width="100sp"
android:layout_height="100sp" />
<Button
android:id="@+id/r2c3_button"
android:layout_column="1"
android:layout_row="3"
android:layout_width="100sp"
android:layout_height="100sp" />
<Button
android:id="@+id/r3c1_button"
android:layout_column="2"
android:layout_row="1"
android:layout_width="100sp"
android:layout_height="100sp" />
<Button
android:id="@+id/r3c2_button"
android:layout_column="2"
android:layout_row="2"
android:layout_width="100sp"
android:layout_height="100sp" />
<Button
android:id="@+id/r3c3_button"
android:layout_column="2"
android:layout_row="3"
android:layout_width="100sp"
android:layout_height="100sp" />
<TextView
android:id="@+id/newgame_text"
android:layout_column="1"
android:layout_row="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/newgame_text" />
<Button
android:id="@+id/button_reset"
android:layout_column="0"
android:layout_row="5"
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"
android:text="@string/button_reset"
android:onClick="resetBoard" />
</GridLayout>
答案 0 :(得分:0)
要重置按钮文本,您应该执行以下操作:
Button btn_reset = (Button) findViewById(R.id.btn_reset);
btn_reset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
resetBoard()
}
});
//在onCreate()之后,用我的替换你的resetBoard()方法......
void resetBoard()
{
square1_button.settext("");
square2_button.settext("");
square3_button.settext("");
square4_button.settext("");
square5_button.settext("");
square6_button.settext("");
square7_button.settext("");
square8_button.settext("");
square9_button.settext("");
}
用你的方法替换我的resetBoard()方法,你就完成了:)
<强>更新强>
如果您真的想使用GridView
,则应通过为buttons
设置Adapter
并设置GridView
LayoutParams
我建议您在xml中使用TableLayout
设置所有按钮,如下所示
<ScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp" >
<Button
android:id="@+id/r1c1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/r1c2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/r1c3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp" >
<Button
android:id="@+id/r2c1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/r2c2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/r2c3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp" >
<Button
android:id="@+id/r3c1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/r3c2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/r3c3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp" >
<Button
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</ScrollView>