我一直在开发Android应用程序,其中我在tablerow中有tablelayout和按钮,我想在我点击时将一些文本放在该按钮上,稍后在时间结束时检索我放入的数据。
这基本上是tic tac toe game有人请帮助我。这是我的第一个Android应用程序。 我以前在基本的java代码中完成了tic tac toe编程,只是尝试使用android app。
`
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#ffffff" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#ffffff" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#ffffff" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#ffffff" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#ffffff" />
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#ffffff" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#ffffff" />
<Button
android:id="@+id/button8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#ffffff" />
<Button
android:id="@+id/button9"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#ffffff" />
</TableRow>
</TableLayout>`
答案 0 :(得分:0)
在onCreate中:
Button NAME = (Button) findViewById(R.id.YOUR_BUTTON_ID);
NAME.setOnClickListener(new OnClickListener(){
@Override
//On click function
public void onClick(View view) {
NAME.setText(“SOMETHING”);
}
});
答案 1 :(得分:0)
在onClick事件的每个触发器的java代码中设置所需的文本,
Button b = (Button) findViewById(R.id.button1);
b.setText("text");
稍后使用
检索该文本String text = b.getText();
答案 2 :(得分:0)
我会重新实现对活动的OnClick监听器,
然后在xml中将onclick作为属性。
点击按钮后,您可以获得按钮ID。
这里有一个例子:
YourActivity extends Activity implements OnClickListener
@Override
public void onClick(View v) {
int id = v.getId();
Button b = (Button) v;
// Your Code
b.setText();
}
然后,在xml中将此行添加到按钮
机器人:的onClick =&#34;的onClick&#34;