0

嗨堆栈的人......

我在让我的 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);

}
}
4

1 回答 1

1

我相信您可以通过为您的 edittext 提供 xml 属性来解决您的问题android:singleLine="true"

于 2013-01-10T20:38:09.307 回答