【问题标题】:OnKeyListener only working within emulatorOnKeyListener 仅在模拟器中工作
【发布时间】:2013-01-09 22:15:02
【问题描述】:

大家好栈......

我在让我的 onKeyListener 工作时遇到了一些问题。基本上正如您在下面看到的,我想在按钮单击以及 EditText 框上的 onKeyListener 上开始意图。

Button click (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);

}
}

【问题讨论】:

  • 什么不起作用?碰撞?没有什么?请说明
  • @jcw 往下输入,在文本编辑框中新建一行。

标签: java android boolean


【解决方案1】:

我相信你可以通过给你的edittext赋予xml属性android:singleLine="true"来解决你的问题

【讨论】:

  • 谢谢,你真是个天才。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 2017-03-30
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
相关资源
最近更新 更多