简体   繁体   中英

How can I also make enter add input text in edittext to my listview? On top of the created add button

I have a button "add" which adds input text from the editText into a listview below. However, can I make it possible to add text to the created list by simply pressing enter? If so, how can i do that in a simple manner?

Name=edittext, list =listview

@Override
        public void onClick(View view) {

            String name = Name.getText().toString();
            if (Name.length() > 0) {
                list.add(name);
                adapter.notifyDataSetChanged();
            }
        }

Quoted from : https://www.stackoverflow.com/a/6832095
Here is what you need to do to capture Enter event on editText :

final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {
          // Perform action on key press
          Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
          return true;
        }
        return false;
    }
});

You need to overwrite your dispatchKeyEvent something like this

@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
    Toast.makeText(UITestsActivity.this,
               "YOU CLICKED ENTER KEY",
                Toast.LENGTH_LONG).show();
        return true;
    }
    return super.dispatchKeyEvent(e);
};

Too long for a comment, therefore adding an answer. Building over previous answer, to achieve your function use this code:

import android.app.LauncherActivity;
import android.content.Context;
import android.content.Intent;
import android.hardware.input.InputManager;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.view.KeyEvent;

import java.util.ArrayList;

public class LoginPage extends AppCompatActivity {

    EditText Name;
    Button Add;
    ListView Lv;
    Button Reset;
    Button Sgame;

    private ImageView information;
    private PopupWindow popupWindow;
    private LayoutInflater layoutInflater;
    private RelativeLayout relativeLayout;

    ArrayList<String> list = new ArrayList<String>();
    ArrayAdapter<String> adapter;

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

        Name = (EditText) findViewById(R.id.Playername);
        Add = (Button) findViewById(R.id.Addbutton);
        Lv = (ListView) findViewById(R.id.list);
        Reset = (Button) findViewById(R.id.Resetbutton);
        Sgame = (Button) findViewById(R.id.Startgamebutton);

        information = (ImageView) findViewById(R.id.info);
        relativeLayout = (RelativeLayout) findViewById(R.id.constrain);

        adapter = new ArrayAdapter<String>(this, R.layout.listviewlayout, R.id.list_content, list);
        Lv.setAdapter(adapter);


        final EditText Name = (EditText) findViewById(R.id.Playername);
        class MyKeyListener implements View.OnKeyListener {
            @Override
            public boolean onKey (View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                        (keyCode == KeyEvent.KEYCODE_ENTER)) {

                    String name = Name.getText().toString();
                    if (Name.length() > 0) {
                        list.add(name);
                        adapter.notifyDataSetChanged();
                    }
                        return true;
                    }
                return false;
                }

        }
        Name.setOnKeyListener(new MyKeyListener());

        Add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                String name = Name.getText().toString();
                if (Name.length() > 0) {
                    list.add(name);
                    adapter.notifyDataSetChanged()
                    ;
                }
            }
        });

        Reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.clear();

            }
        });

        Sgame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!list.isEmpty())
                    openActivity2();
            }
        });

        information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.informationpopup, null);

                popupWindow = new PopupWindow(container, 1150, 2000, true);
                popupWindow.showAtLocation(relativeLayout, Gravity.NO_GRAVITY, 140, 300);

                container.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        popupWindow.dismiss();
                        return true;
                    }
                });

            }
        });
    }

    public void openActivity2() {
        Intent intent = new Intent(this, Gamescreen.class);
        String s= list.get(0);
        intent.putExtra("Name1", s);
        startActivity(intent);
    }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM