显示单词的解决方案:我找到了StringBuilder,而不是可扩展的字符串(隐藏和显示randomWord的一部分),连同indexOf和userinput,我能够在用户猜测后输入正确的字母。像这样:
public void guess()
{
char guess = userInput.getText().charAt(0);
StringBuilder builder = new StringBuilder(hiddenWord);
String j = ""+guess;
int index = randomedWord.indexOf(guess);
if (randomedWord.contains(j))
{
while (index >= 0)
{
builder.setCharAt(index*2, guess);
index = randomedWord.indexOf(guess, index + 1);
hiddenWord = builder.toString().trim();
wordHolder.setText(hiddenWord);
if (!(hiddenWord.toString().contains("_".toString())) )
{
winner();
}
}
}
else
{
showImages();
}
}
随机字的解决方案(生成它):看看詹姆斯的回答,我已经在我的数据库上返回了游标本身,而不是我想要的列的字符串值,这是正确的方法
db.open();
wordHolder.setText(db.randomize());
db.close();
public String randomize() {
Cursor cursor;
cursor = this.db.query("tblnames ORDER BY RANDOM() LIMIT 1", new String[] { "*" }, null, null, null, null, null);
if(cursor.moveToFirst()) {
return cursor.getString(cursor.getColumnIndexOrThrow( "word" ));
}
return "";
}
完整代码:
// MainActivity.java
package com.emanuelolsson.simplehangman;
import database.DBAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class HangMan extends Activity {
// DECLARE NEEDED VARIABLES
private Button newGameButton, enterLetterButton;
private EditText userInput;
private TextView wordHolder;
private ImageView imageOne, imageTwo, imageThree, imageFour, imageFive, imageSix, imageSeven, imageEight, winner, hanged;
private String hiddenWord, randomedWord;
private int count = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hang_man);
DBAdapter db = new DBAdapter(this);
// ASSIGN OBJECTS
newGameButton = (Button) findViewById(R.id.newGame);
enterLetterButton = (Button) findViewById(R.id.enter);
wordHolder = (TextView) findViewById(R.id.wordHolder);
userInput = (EditText) findViewById(R.id.userInput);
imageOne = (ImageView) findViewById(R.id.imageView1);
imageTwo = (ImageView) findViewById(R.id.imageView2);
imageThree = (ImageView) findViewById(R.id.imageView3);
imageFour = (ImageView) findViewById(R.id.imageView4);
imageFive = (ImageView) findViewById(R.id.imageView5);
imageSix = (ImageView) findViewById(R.id.imageView6);
imageSeven = (ImageView) findViewById(R.id.imageView7);
imageEight = (ImageView) findViewById(R.id.imageView8);
winner = (ImageView) findViewById(R.id.winner);
hanged = (ImageView) findViewById(R.id.hanged);
imageOne.setVisibility(View.INVISIBLE);
imageTwo.setVisibility(View.INVISIBLE);
imageThree.setVisibility(View.INVISIBLE);
imageFour.setVisibility(View.INVISIBLE);
imageFive.setVisibility(View.INVISIBLE);
imageSix.setVisibility(View.INVISIBLE);
imageSeven.setVisibility(View.INVISIBLE);
imageEight.setVisibility(View.INVISIBLE);
winner.setVisibility(View.INVISIBLE);
hanged.setVisibility(View.INVISIBLE);
wordHolder.setVisibility(View.VISIBLE);
// ADD LISTENERS
newGameButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
newGame();
}
});
enterLetterButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
if (!(userInput.getText().toString().isEmpty()) )
{
guess();
} else if (userInput.getText().toString().isEmpty())
{
Toast toast = Toast.makeText(getApplicationContext(), "You need to insert a letter", Toast.LENGTH_SHORT);
toast.show();
}
}
});
db.open();
db.fill();
randomedWord = db.randomize();
invWord();
wordHolder.setText(hiddenWord);
db.clear();
db.close();
}
public void newGame() {
DBAdapter db = new DBAdapter(this);
count = 0;
imageOne.setVisibility(View.INVISIBLE);
imageTwo.setVisibility(View.INVISIBLE);
imageThree.setVisibility(View.INVISIBLE);
imageFour.setVisibility(View.INVISIBLE);
imageFive.setVisibility(View.INVISIBLE);
imageSix.setVisibility(View.INVISIBLE);
imageSeven.setVisibility(View.INVISIBLE);
imageEight.setVisibility(View.INVISIBLE);
winner.setVisibility(View.INVISIBLE);
hanged.setVisibility(View.INVISIBLE);
db.open();
db.fill();
randomedWord = db.randomize();
invWord();
wordHolder.setText(hiddenWord);
db.clear();
db.close();
}
public void invWord()
{
hiddenWord = randomedWord;
hiddenWord = hiddenWord.replaceAll(".", "_" +" ");
}
public void guess()
{
char guess = userInput.getText().charAt(0);
StringBuilder builder = new StringBuilder(hiddenWord);
String j = ""+guess;
int index = randomedWord.indexOf(guess);
if (randomedWord.contains(j))
{
while (index >= 0)
{
builder.setCharAt(index*2, guess);
index = randomedWord.indexOf(guess, index + 1);
hiddenWord = builder.toString().trim();
wordHolder.setText(hiddenWord);
if (!(hiddenWord.toString().contains("_".toString())) )
{
winner();
}
}
}
else
{
showImages();
}
}
private void winner()
{
imageOne.setVisibility(View.INVISIBLE);
imageTwo.setVisibility(View.INVISIBLE);
imageThree.setVisibility(View.INVISIBLE);
imageFour.setVisibility(View.INVISIBLE);
imageFive.setVisibility(View.INVISIBLE);
imageSix.setVisibility(View.INVISIBLE);
imageSeven.setVisibility(View.INVISIBLE);
imageEight.setVisibility(View.INVISIBLE);
winner.setVisibility(View.VISIBLE);
}
private void showImages() {
count ++;
if (count == 1)
{
imageOne.setVisibility(View.VISIBLE);
}
else if (count == 2)
{
imageTwo.setVisibility(View.VISIBLE);
}
else if (count == 3)
{
imageThree.setVisibility(View.VISIBLE);
}
else if (count == 4)
{
imageFour.setVisibility(View.VISIBLE);
}
else if (count == 5)
{
imageFive.setVisibility(View.VISIBLE);
}
else if (count == 6)
{
imageSix.setVisibility(View.VISIBLE);
}
else if (count == 7)
{
imageSeven.setVisibility(View.VISIBLE);
}
else if (count == 8)
{
gameEnd();
}
}
private void gameEnd()
{
imageOne.setVisibility(View.INVISIBLE);
imageTwo.setVisibility(View.INVISIBLE);
imageThree.setVisibility(View.INVISIBLE);
imageFour.setVisibility(View.INVISIBLE);
imageFive.setVisibility(View.INVISIBLE);
imageSix.setVisibility(View.INVISIBLE);
imageSeven.setVisibility(View.INVISIBLE);
imageEight.setVisibility(View.VISIBLE);
hanged.setVisibility(View.VISIBLE);
wordHolder.setText(randomedWord);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_hang_man, menu);
return true;
}
}
// XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picOne"
android:src="@drawable/one" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picTwo"
android:src="@drawable/two" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picThree"
android:src="@drawable/three" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picFour"
android:src="@drawable/four" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picFive"
android:src="@drawable/five" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picSix"
android:src="@drawable/six" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picSeven"
android:src="@drawable/seven" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picEight"
android:src="@drawable/eight" />
<ImageView
android:id="@+id/winner"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignTop="@+id/imageView1"
android:layout_centerHorizontal="true"
android:contentDescription="@string/winner"
android:src="@drawable/winner" />
<ImageView
android:id="@+id/hanged"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignTop="@+id/imageView1"
android:layout_centerHorizontal="true"
android:contentDescription="@string/hanged"
android:src="@drawable/hanged" />
<TextView
android:id="@+id/wordHolder"
android:layout_width="175dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:layout_alignTop="@+id/imageView1"
android:layout_marginTop="134dp"
android:layout_marginLeft="50dp"
android:ems="10"
android:textSize="30dp" />
<Button
android:id="@+id/newGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/wordHolder"
android:layout_centerVertical="true"
android:text="@string/newGame" />
<Button
android:id="@+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/newGame"
android:layout_alignBottom="@+id/newGame"
android:layout_toLeftOf="@+id/newGame"
android:text="@string/enter" />
<EditText
android:id="@+id/userInput"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignBaseline="@+id/enter"
android:layout_alignBottom="@+id/enter"
android:layout_toLeftOf="@+id/enter"
android:ems="10"
android:inputType="text"
android:textSize="20dp" >
<requestFocus />
</EditText>
<ImageView
android:id="@+id/imageView9"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="@+id/header"
android:src="@drawable/header" />
</RelativeLayout>