【问题标题】:Wait for user input?等待用户输入?
【发布时间】:2023-07-23 01:11:01
【问题描述】:

我正在尝试将我用 Java 编写的基于文本的终端 rpg 重新设计为 Android 应用程序。我正在制作游戏的一小部分,玩家可以在其中从一个房间走到另一个房间。

代码会停止并提示用户在终端应用程序中输入,现在它只是一直运行到最后,不会接受任何输入。

public class MainActivity extends AppCompatActivity {

//rooms
Room r1 = new Room("The Great Hall", "A long wide room draped here and there in curtains.");
Room r2 = new Room("The Side Study", "a small cluttered room.");
Room r3 = new Room("The Gardens", "a misty path between beds, once carefully tended now overgrown and disturbed by the encroaching weeds.");
Room r4 = new Room("A Long Passage", "a narrow hallway lined with brooding portraits. Set into the walls to your left and right are blank grey doors, three to a side. They are locked.");
Room currentRoom;

//room maps
HashMap<String, Room> mapR1 = new HashMap<>();
HashMap<String, Room> mapR2 = new HashMap<>();
HashMap<String, Room> mapR3 = new HashMap<>();
HashMap<String, Room> mapR4 = new HashMap<>();

//user
String userDir;
String endgame;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //begin setup
    //

    //map for great hall
    mapR1.put("north", r4);
    mapR1.put("east", r2);
    mapR1.put("west", r3);
    r1.setRoomMap(mapR1);

    //map for side study
    mapR2.put("west", r1);
    r2.setRoomMap(mapR2);

    //map for gardens
    mapR3.put("east", r1);
    r3.setRoomMap(mapR3);

    //map for long passage
    mapR4.put("south", r1);
    r4.setRoomMap(mapR4);

    //init
    currentRoom = r1;
    endgame = "non";

    //views
    final TextView textView = (TextView) findViewById(R.id.youmum);
    final EditText editText = (EditText) findViewById(R.id.editText);

    //
    //end setup

    //the actual game play
    //

    assert textView != null;
    textView.setText("You are in the " + currentRoom.getRoomTitle() + "; " + currentRoom.getRoomDescription());
    textView.append("\nThere are exits to the, ");

    for (String ky : currentRoom.getRoomMap().keySet()) {
        textView.append(ky + " ");
    }

    textView.append("\nChoose a direction");

    assert editText != null;
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                sendMessage();
                handled = true;
            }
            return handled;
        }

        private void sendMessage() {
            userDir = editText.getText().toString().toLowerCase();
            if (userDir.contains("go") || userDir.contains("walk") || userDir.contains("move")) {
                boolean notDir = false;
                for (String kee : currentRoom.getRoomMap().keySet()) {
                    if (userDir.contains(kee)) {
                        currentRoom = currentRoom.getExitRoom(kee);
                        notDir = false;
                        break;
                    } else {
                        notDir = true;
                    }
                }
                if (notDir) {
                    textView.setText("You cannot go that way.\n");
                }
            }
        }

    });

    textView.setText("You are in the " + currentRoom.getRoomTitle() + "; " + currentRoom.getRoomDescription());
    textView.append("\nThere are exits to the, ");

    for (String ky : currentRoom.getRoomMap().keySet()) {
        textView.append(ky + " ");
    }

    textView.append("\nChoose a direction");

  }
 }

我对 Android 很陌生(这实际上是我的第一次尝试)所以我假设我只是错过了一些明显的东西。

【问题讨论】:

    标签: java android user-input


    【解决方案1】:

    底部的代码(显示有关您已搬入的新房间的文本)需要移动到 ActionListener 中,以便在每次事件触发时运行它,而不仅仅是在初始化所有内容时运行一次。您可以在更新 currentRoom 的现有代码之后将其放在 sendMessage() 中。

    您可能已经这样做了,但是为了触发 IME_ACTION_SEND 事件,定义 EditText 的 XML 应该包括:

    android:imeOptions="actionSend"
    

    您可能还想添加一些代码来清除 EditText 内容,因为在用户点击“发送”后,该内容当前会保留在那里。

    【讨论】: