[英]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. 我有一个“添加”按钮,它将来自
editText
输入文本添加到下面的listview
中。 However, can I make it possible to add text to the created list by simply pressing enter? 但是,是否可以仅通过按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 引用自: https : //www.stackoverflow.com/a/6832095
Here is what you need to do to capture Enter event on editText : 您需要执行以下操作以捕获editText上的Enter事件:
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 您需要像这样覆盖您的dispatchKeyEvent
@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);
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.