gpt4 book ai didi

java - OnKeyListener 仅在模拟器中工作

转载 作者:行者123 更新时间:2023-12-01 14:56:51 28 4
gpt4 key购买 nike

堆栈的人们大家好......

我在让 onKeyListener 工作时遇到一些问题。基本上,正如您在下面看到的,我想启动按钮单击以及 EditText 框上的 KeyListener 的 Intent 。

按钮单击(public void sendip)工作正常,onKeyListener(监听回车键)在模拟器中也工作正常。但当我尝试在实际的 Android 设备上按“Enter”键时,它就不起作用了。

无论如何,有任何想法......将不胜感激......

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"

关于java - OnKeyListener 仅在模拟器中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14247478/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com