我有一个按钮"添加"这会将editText
中的输入文本添加到下面的listview
中。但是,我是否可以通过按Enter键将文本添加到创建的列表中?如果是这样,我怎么能以简单的方式做到这一点?
Name=edittext, list =listview
@Override
public void onClick(View view) {
String name = Name.getText().toString();
if (Name.length() > 0) {
list.add(name);
adapter.notifyDataSetChanged();
}
}
答案 0 :(得分:1)
引自:https://www.stackoverflow.com/a/6832095
以下是在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;
}
});
答案 1 :(得分:0)
你需要覆盖像这样的dispatchKeyEvent
photo = PhotoImage(file="*your image file*")
Button(master, command=Cookie, image=photo).grid(row=2, column=1, sticky=W, pady=4)
答案 2 :(得分:0)
评论太长,因此添加答案。建立以前的答案,实现您的功能使用此代码:
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);
}}