这是我在教程中找到并修改过的益智游戏的代码。
现在我要展示feedbackText.setText("we have a winner");
&安培; moveCounter.setText(Integer.toString(Integer.parseInt((String) moveCounter.getText())+1));
也表明了这一点。
在新的页面中。我怎么展示它?
另一个问题是我可以为这个应用添加触摸声吗?
public class Game extends Activity {
private TextView moveCounter;
private TextView feedbackText;
private Button[] buttons;
private Boolean bad_move=false;
private static final Integer[] goal = new Integer[] {0,1,2,3,4,5,6,7,8};
private ArrayList<Integer> cells = new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
buttons=findButtons();
for(int i=0;i<9;i++)
{
this.cells.add(i);
}
Collections.shuffle(this.cells); //random cells array
fill_grid();
moveCounter = (TextView) findViewById(R.id.MoveCounter);
feedbackText = (TextView) findViewById(R.id.FeedbackText);
for (int i = 1; i < 9; i++) {
buttons[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
makeMove((Button) v);
}
});
}
moveCounter.setText("0");
feedbackText.setText(R.string.game_feedback_text);
}
public Button[] findButtons() {
Button[] b = new Button[9];
b[0] = (Button) findViewById(R.id.Button00);
b[1] = (Button) findViewById(R.id.Button01);
b[2] = (Button) findViewById(R.id.Button02);
b[3] = (Button) findViewById(R.id.Button03);
b[4] = (Button) findViewById(R.id.Button04);
b[5] = (Button) findViewById(R.id.Button05);
b[6] = (Button) findViewById(R.id.Button06);
b[7] = (Button) findViewById(R.id.Button07);
b[8] = (Button) findViewById(R.id.Button08);
return b;
}
public void makeMove(final Button b) {
bad_move=true;
int b_text,b_pos,zuk_pos;
b_text=Integer.parseInt((String) b.getText());
b_pos=find_pos(b_text);
zuk_pos=find_pos(0);
switch(zuk_pos)
{
case(0):
if(b_pos==1||b_pos==3)
bad_move=false;
break;
case(1):
if(b_pos==0||b_pos==2||b_pos==4)
bad_move=false;
break;
case(2):
if(b_pos==1||b_pos==5)
bad_move=false;
break;
case(3):
if(b_pos==0||b_pos==4||b_pos==6)
bad_move=false;
break;
case(4):
if(b_pos==1||b_pos==3||b_pos==5||b_pos==7)
bad_move=false;
break;
case(5):
if(b_pos==2||b_pos==4||b_pos==8)
bad_move=false;
break;
case(6):
if(b_pos==3||b_pos==7)
bad_move=false;
break;
case(7):
if(b_pos==4||b_pos==6||b_pos==8)
bad_move=false;
break;
case(8):
if(b_pos==5||b_pos==7)
bad_move=false;
break;
}
if(bad_move==true)
{
feedbackText.setText("Move Not Allowed");
return;
}
feedbackText.setText("Move OK");
cells.remove(b_pos);
cells.add(b_pos, 0);
cells.remove(zuk_pos);
cells.add(zuk_pos,b_text);
fill_grid();
moveCounter.setText(Integer.toString(Integer.parseInt((String) moveCounter.getText())+1));
for(int i=0;i<9;i++)
{
if(cells.get(i)!=goal[i])
{
return;
}
}
feedbackText.setText("we have a winner");
}
public void fill_grid()
{
for(int i=0;i<9;i++)
{
int text=cells.get(i);
AbsoluteLayout.LayoutParams absParams =
(AbsoluteLayout.LayoutParams)buttons[text].getLayoutParams();
switch(i)
{case(0):
absParams.x = 15;
absParams.y = 15;
buttons[text].setLayoutParams(absParams);
break;
case(1):
absParams.x = 170;
absParams.y = 15;
buttons[text].setLayoutParams(absParams);
break;
case(2):
absParams.x = 325;
absParams.y = 15;
buttons[text].setLayoutParams(absParams);
break;
case(3):
absParams.x = 15;
absParams.y = 185;
buttons[text].setLayoutParams(absParams);
break;
case(4):
absParams.x =170;
absParams.y =185;
buttons[text].setLayoutParams(absParams);
break;
case(5):
absParams.x =325;
absParams.y =185;
buttons[text].setLayoutParams(absParams);
break;
case(6):
absParams.x = 15;
absParams.y = 355;
buttons[text].setLayoutParams(absParams);
break;
case(7):
absParams.x = 170;
absParams.y = 355;
buttons[text].setLayoutParams(absParams);
break;
case(8):
absParams.x = 325;
absParams.y = 355;
buttons[text].setLayoutParams(absParams);
break;
}
}
}
public int find_pos(int element)
{
int i;
for(i=0;i<9;i++)
{
if(cells.get(i)==element)
{
break;
}
}
return i;
}
}
答案 0 :(得分:0)
制作新页面。例如,将其命名为“WinnerActivity”
public void navigateToWinnerPage() {
startActivity(new Intent(PuzzelActivity.this, WinnerActivity.class));
}
此代码将帮助您转到获胜者页面。 在那里你可以设置“找到赢家”之类的文字。
答案 1 :(得分:0)
use this code
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.showwinner, null);
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
dialog = builder.create();
Button btnOk = (Button) dialoglayout.findViewById(R.id.btnOk);
TextView tv = (TextView) dialoglayout.findViewById(R.id.txtText2);
tv.settext(moveCounter.getText());
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.getWindow().getAttributes().windowAnimations =R.style.dialog_animation;
dialog.show();
and add showwinner.xml into your layout folder
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="We Have a Winner !!!!!"
android:id="@+id/txtText"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:gravity="center_vertical|center_horizontal"
android:padding="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/txtText2"
android:layout_gravity="center_horizontal"
android:textSize="15sp"
android:gravity="center_vertical|center_horizontal"
android:padding="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
android:id="@+id/btnOk"
android:layout_gravity="center_horizontal" />