1

我是 android 开发新手,在 Hang-Man 游戏中遇到了一些问题。

这就是我希望它工作的方式: 1. 当应用程序启动时,打开数据库 (db.open()) 并从数据库 (db.random()) 中获取一个随机单词并放入生成的单词字符串。然后将 generatedWord 制作成 Spannablestring 文本 (SpannableString text = new SpannableString(generatedWord);) 并将整个文本制作成白色/不可见的颜色。

  1. 当用户在 textview userInput 中输入一个字符时,我想通过使用 onKeyListener 将它保存到 lastInput 变量:lastInput = (char)event.getUnicodeChar();

  2. 然后当按下 Enter 按钮时,应该调用 guess() 来发送 lastInput 的参数。在那里它检查 lastInput 是否在 indexOf generatedWord 中,如果是,它将使字符的文本再次变黑并再次可见,如果不是,它应该调用 showImages() 从而使图像可见。

事情是这样的!数据库正在工作,我刚刚让应用程序与 gui 和所有东西一起运行,然后我做了一些小改动,现在我无法再次启动应用程序,只会出现致命错误。我做错了什么,我能想到什么?

非常感谢

// 主要活动

    package com.emanuelolsson.simplehangman;

import database.DBAdapter;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
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;

public class HangMan extends Activity {

    // DECLARE NEEDED VARIABLES
    private String generatedWord = "";
    private char lastInput = '\0';  
    private Button newGameButton, enterLetterButton;
    private EditText wordHolder, userInput;
    private ImageView imageOne, imageTwo, imageThree, imageFour, imageFive, imageSix, imageSeven, imageEight, winner, hanged;
    private SpannableString text = new SpannableString(generatedWord);
    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 = (EditText) 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);


        // ADD LISTENERS
        newGameButton.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) {

            }
        });

        enterLetterButton.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) {

                guess(lastInput);
            }
        });

        userInput.setOnKeyListener(new View.OnKeyListener(){
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                lastInput = (char)event.getUnicodeChar();
                return false;
            }
        });

        db.open();
        db.randomize(generatedWord);
                SpannableString text = new SpannableString(generatedWord);
        text.setSpan(new ForegroundColorSpan(Color.WHITE), 0, text.length(), 0);  
        wordHolder.setText(text);
        db.close();
    }


// What happens when user clicks Enter
    // If the guess (char) is in the index of generated word and not -1 it will span back hopefully to all char
    public void guess(char guess) {
        guess = Character.toUpperCase(guess);
        if (generatedWord.indexOf(lastInput) != -1) {
            text.setSpan(new ForegroundColorSpan(Color.BLACK), text.charAt(lastInput), text.charAt(lastInput), 0);
            //guessedChars.add(guess);   
        } else {
            showImages();
        }
    }

    private void showImages() {

        count ++;

        if (count == 1) 
        {
            imageOne.setVisibility(View.INVISIBLE);
        }
        else if (count == 2)
        {
            imageTwo.setVisibility(View.INVISIBLE);
        }
        else if (count == 3) 
        {
            imageThree.setVisibility(View.INVISIBLE);
        }
        else if (count == 4)
        {
            imageFour.setVisibility(View.INVISIBLE);
        }
        else if (count == 5) 
        {
            imageFive.setVisibility(View.INVISIBLE);
        }
        else if (count == 6)
        {
            imageSix.setVisibility(View.INVISIBLE);
        }
        else if (count == 7) 
        {
            imageSeven.setVisibility(View.INVISIBLE);
        }
        else if (count == 8)
        {
            gameEnd();
        }

    }

    private void gameEnd() {
        if (count == 8)
        {
            hanged.setVisibility(View.VISIBLE);
        }
        else
        {
            winner.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_hang_man, menu);
        return true;
    }
}

// 数据库适配器

    package database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter 
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_WORDS = "words";  
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "dbnames";
    private static final String DATABASE_TABLE = "tblnames";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table tblnames (_id integer primary key autoincrement, "
        + "words text not null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS tblnames");
            onCreate(db);
        }
    }    

    // Open DB
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    // Close DB  
    public void close() 
    {
        DBHelper.close();
    }

    // INsert name
    public long insertWord(String word) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_WORDS, word);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    // Delete a name
    public boolean deleteWord(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + 
                "=" + rowId, null) > 0;
    }

    // Get all names
    public Cursor getAllWords() 
    {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, 
                KEY_WORDS}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }


    // Get a certain name
    public Cursor getWord(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                        KEY_ROWID,
                        KEY_WORDS}, 
                        KEY_ROWID + "=" + rowId, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    // Update name
    public boolean updateWord(long rowId, String word) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_WORDS, word);

        return db.update(DATABASE_TABLE, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }

    // Clears DB
    public void clear() {

        db.delete(DATABASE_TABLE, "1", null);

    }

    public void randomize(String word) {
        @SuppressWarnings("unused")
        Cursor cursor;
        cursor =  this.db.query("tblnames ORDER BY RANDOM() LIMIT 1", new String[] { "*" }, null, null, null, null, null);
    }
}

// 布局

    <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:src="@drawable/one"
        android:contentDescription="@string/picOne" />

    <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:src="@drawable/two" 
        android:contentDescription="@string/picTwo" />

    <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:src="@drawable/four" 
        android:contentDescription="@string/picFour" />

    <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:src="@drawable/five"
        android:contentDescription="@string/picFive" />

    <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:src="@drawable/six"
        android:contentDescription="@string/picSix" />

    <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:src="@drawable/seven"
        android:contentDescription="@string/picSeven" />

    <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:src="@drawable/eight"
        android:contentDescription="@string/picEight" />

    <EditText
        android:id="@+id/wordHolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:inputType="text" >
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/enter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/wordHolder"
        android:layout_marginTop="28dp"
        android:text="@string/enter" />

    <EditText
        android:id="@+id/userInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/enter"
        android:layout_alignLeft="@+id/wordHolder"
        android:layout_alignTop="@+id/enter"
        android:layout_toLeftOf="@+id/enter"
        android:ems="10"
        android:inputType="text" />

    <ImageView
        android:id="@+id/winner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:src="@drawable/winner"
        android:contentDescription="@string/winner" />

    <ImageView
        android:id="@+id/hanged"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:src="@drawable/hanged"
        android:contentDescription="@string/hanged" />

    <Button
        android:id="@+id/newGame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/enter"
        android:layout_alignBottom="@+id/enter"
        android:layout_toRightOf="@+id/enter"
        android:text="@string/newGame" />

</RelativeLayout>
4

2 回答 2

1

解决方案:再次检查猜测是否显示正确的字母猜测,从 _ 到适当的字母。

// 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>
于 2012-10-24T21:08:08.123 回答
0

为什么不这样做呢?

String wordToGuess = "ANDROID";
Set<Character> guessedChars = new HashSet<Character>();

// call this with the user-input
//
public void guess(char guess) {
    guess = Character.toUpperCase(guess);
    if (wordToGuess.indexOf(guess) != -1) {
        guessedChars.add(guess);
    }
}

// call this afterwards to get string with chars guessed so far and dots for not-yet-guessed characters
//
public String guessedSoFar() {
    String result = "";
    for (char ch: wordToGuess.toCharArray()) {
        if (guessedChars.contains(ch)) {
            result += ch;
        } else {
            result += ".";   // . for not yet guessed words
        }
    }
    return result;
}   
于 2012-09-22T16:12:59.287 回答