1

私は新しい Android 開発者で、Hang-Man ゲームでいくつかの問題を抱えています。

これが私がそれをどのように機能させたいかです: 次に、生成された単語は Spannablestring テキスト (SpannableString テキスト = 新しい SpannableString(generatedWord);) になり、テキスト全体が白/非表示になります。

  1. ユーザーがテキストビューのuserInputにcharを入力すると、onKeyListenerを使用してlastInput vairableに保存したい: lastInput = (char)event.getUnicodeChar();

  2. 次に、Enter ボタンが押されたときに、引数 lastInput を送信するguess() を呼び出す必要があります。そこで、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;
    }
}

// DBAdapter

    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 に答える