OnKeyListener仅在模拟器中工作

时间:2013-01-09 22:15:03

标签: java android boolean

喜欢堆栈的人......

我在让onKeyListener工作方面遇到了一些问题。基本上,如下所示,我想在按钮单击上启动一个意图,并在EditText框上启动onKeyListener。

按钮单击(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);

}
}

1 个答案:

答案 0 :(得分:1)

我相信您可以通过为edittext提供xml属性android:singleLine="true"

来解决您的问题