Java OnKeyListener仅在emulator中工作
嗨,斯塔克的人们 我在让onKeyListener工作方面遇到了一些问题。基本上,正如你在下面看到的,我想在编辑文本框中启动一个按钮点击和键监听的意图 单击按钮(public void sendip)可以正常工作,onKeyListener(侦听enter键)也可以在模拟器中正常工作。但当我在实际的安卓设备上尝试按下“回车”键时,它就不起作用了 不管怎样,我有什么想法。。。。。非常感谢Java OnKeyListener仅在emulator中工作,java,android,boolean,Java,Android,Boolean,嗨,斯塔克的人们 我在让onKeyListener工作方面遇到了一些问题。基本上,正如你在下面看到的,我想在编辑文本框中启动一个按钮点击和键监听的意图 单击按钮(public void sendip)可以正常工作,onKeyListener(侦听enter键)也可以在模拟器中正常工作。但当我在实际的安卓设备上尝试按下“回车”键时,它就不起作用了 不管怎样,我有什么想法。。。。。非常感谢 package com.smarte.smartipcontrol; import android.app.
package com.smarte.smartipcontrol;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
public class IPEntry extends Activity implements OnKeyListener {
public final static String ACTUALSMARTIP = "com.smarte.smartipcontrol.ACTU_IP";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_ipentry);
savediP =(EditText)findViewById(R.id.serverIpAddress);
savediP.setOnKeyListener(this);
}
protected void onResume() {
super.onResume();
SharedPreferences prefs = getPreferences(0);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
savediP.setText(restoredText, TextView.BufferType.EDITABLE);
int selectionStart = prefs.getInt("selection-start", -1);
int selectionEnd = prefs.getInt("selection-end", -1);
if (selectionStart != -1 && selectionEnd != -1) {
savediP.setSelection(selectionStart, selectionEnd);
}
}
}
protected void onPause() {
super.onPause();
SharedPreferences.Editor editor = getPreferences(0).edit();
editor.putString("text", savediP.getText().toString());
editor.putInt("selection-start", savediP.getSelectionStart());
editor.putInt("selection-end", savediP.getSelectionEnd());
editor.commit();
}
private EditText savediP;
//@Override
//public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.act_ipentry, menu);
//return true;
//}
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == EditorInfo.IME_ACTION_SEARCH ||
keyCode == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (!event.isShiftPressed()) {
Log.v("AndroidEnterKeyActivity","Enter Key Pressed!");
switch (view.getId()) {
case R.id.serverIpAddress:
Intent intent = new Intent(this, IPControl.class);
EditText ipaddress = (EditText) findViewById(R.id.serverIpAddress);
String actu_ip = ipaddress.getText().toString();
intent.putExtra(ACTUALSMARTIP, actu_ip);
startActivity(intent);
break;
}
return true;
}
}
return false; // pass on to other listeners.
}
/** Called when the user clicks the SendIP button */
public void sendip (View view) {
Intent intent = new Intent(this, IPControl.class);
EditText ipaddress = (EditText) findViewById(R.id.serverIpAddress);
String actu_ip = ipaddress.getText().toString();
intent.putExtra(ACTUALSMARTIP, actu_ip);
startActivity(intent);
}
}
我相信,您可以通过为edittext提供xml属性来解决您的问题。
android:singleLine=“true”
什么不起作用?崩溃没有什么?请specify@jcw它向下输入,在文本编辑框中创建一个新行。谢谢,你真是个天才。:)