我的TextView.OnEditorActionListener()不起作用

时间:2019-06-10 20:00:27

标签: java android xml android-layout

我正在为游戏指挥创建一个应用程序。在此游戏中,指挥必须在EditText框中输入每个玩家的姓名,这些姓名将保存在字符串数组中,以备将来分配角色。

我的EditText id是addplayer,并且我已经为其分配了setOnEditorActionListener()方法。

我在其主体中添加了此条件if (actionId == EditorInfo.IME_ACTION_NEXT)

虽然我的命令都不起作用。

public class FirstActivity extends AppCompatActivity implements View.OnClickListener {

    static List<String> names = new ArrayList<>();
    static int playerSum = 0;

    RelativeLayout firstActLayout;
    TextView numberOfPlayers;
    EditText addPlayer;
    TextView doneButton;
    TextView settingsButton;
    InputMethodManager imm;

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

        firstActLayout = findViewById(R.id.first_act_layout);
        numberOfPlayers = findViewById(R.id.number_of_players);
        addPlayer = findViewById(R.id.add_player);
        doneButton = findViewById(R.id.done_button);
        settingsButton = findViewById(R.id.settings_button);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_player:
                addPlayers();
                break;
            case R.id.done_button:
                openSecondActivity();
                break;
            case R.id.settings_button:

                break;
        }
    }

    public void addPlayers (){
        addPlayer.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_NEXT) {
                    // add player's name to the name array.
                    names.add(addPlayer.getText().toString());
                    // increment the total number of players.
                    playerSum++;
                    // display the total number of players next the hash tag.
                    numberOfPlayers.append(Integer.toString(playerSum), 8, 12);
                    // keep the soft keyboard available for the next entry.
                    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                }
                return false;
            }
        });
    }
}

XML:

    <EditText
        android:id="@+id/add_player"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginBottom="3dp"
        android:autofillHints="@string/enter_name"
        android:background="#fff"
        android:layout_centerHorizontal="true"
        android:hint="@string/enter_name"
        android:inputType="text"
        android:clickable="true"
        android:focusable="true"
        android:imeOptions="actionNext"
        android:singleLine="true"
        android:textColor="#FF030303"
        tools:targetApi="o"
        />

我希望指挥能够: 1.在EditText中输入名称 2.并按下软键盘上的下一个图标。

公共无效的addPlayers()应该: 1.以添加的名称并将其分配给名称[]数组, 2.删除下一个条目的EditText框, 3.使用playerSum ++增加玩家数量, 4.在TextView框中显示玩家总数(playerSum ++)(numberOfPlayers)。 5.保持软键盘上显示您的名字。

但是什么也没发生。播放器的名称位于EditText框中。

1 个答案:

答案 0 :(得分:0)

好吧,我刚刚意识到我出了错。我忘了添加以下3行:

    addPlayer.setOnClickListener(this);
    doneButton.setOnClickListener(this);
    settingsButton.setOnClickListener(this);

当我考虑这个问题时,这个问题非常愚蠢,大声笑。没有设置方法的按钮将如何工作? h。