单击按钮打开键盘
我确实在单击按钮时打开键盘,并将所有按键值存储在字符串中,但我遇到了这样的问题
1. when i press CAPITAL keys then i will got 2 times char
2. when i press search and Menu button then also enter the key
3. some char i can't get like PIE
,所以请检查此代码并给我正确的答案。 我的代码是:
package com.indianic.phykeyboard;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class KeyboardActivity extends Activity {
EditText mEdit;
Boolean key = false;
Button mKey, mMon;
String getvalue;
InputMethodManager imm;
StringBuilder stringBuilder;
TextView value;
String ch = "";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mKey = (Button) findViewById(R.id.openkey);
value = (TextView) findViewById(R.id.value);
mKey.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, 0);
stringBuilder = new StringBuilder();
}
});
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
return true;
}
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.v("log_tag", "char : " + event.getUnicodeChar());
if (event.getUnicodeChar() != 0) {
int i = event.getUnicodeChar();
ch = new Character((char) i).toString();
}
if (ch.length() > 0) {
if (keyCode == KeyEvent.KEYCODE_DEL && stringBuilder.length() >= 1) {
stringBuilder.delete(stringBuilder.length() - 1, stringBuilder
.length());
} else if (keyCode != KeyEvent.KEYCODE_DEL
&& keyCode != KeyEvent.KEYCODE_ENTER) {
stringBuilder.append(ch);
} else if (keyCode == KeyEvent.KEYCODE_SEARCH
|| keyCode == KeyEvent.KEYCODE_MENU) {
}
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
stringBuilder.delete(0, stringBuilder.length());
finish();
}
String str = stringBuilder.toString().trim();
value.setText(str);
return false;
}
}
I do open Keyboard on Button click and i store all keypress value in String but i am facing some problem like this
1. when i press CAPITAL keys then i will got 2 times char
2. when i press search and Menu button then also enter the key
3. some char i can't get like PIE
So please check this code and give me proper answer.
My Code is :
package com.indianic.phykeyboard;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class KeyboardActivity extends Activity {
EditText mEdit;
Boolean key = false;
Button mKey, mMon;
String getvalue;
InputMethodManager imm;
StringBuilder stringBuilder;
TextView value;
String ch = "";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mKey = (Button) findViewById(R.id.openkey);
value = (TextView) findViewById(R.id.value);
mKey.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, 0);
stringBuilder = new StringBuilder();
}
});
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
return true;
}
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.v("log_tag", "char : " + event.getUnicodeChar());
if (event.getUnicodeChar() != 0) {
int i = event.getUnicodeChar();
ch = new Character((char) i).toString();
}
if (ch.length() > 0) {
if (keyCode == KeyEvent.KEYCODE_DEL && stringBuilder.length() >= 1) {
stringBuilder.delete(stringBuilder.length() - 1, stringBuilder
.length());
} else if (keyCode != KeyEvent.KEYCODE_DEL
&& keyCode != KeyEvent.KEYCODE_ENTER) {
stringBuilder.append(ch);
} else if (keyCode == KeyEvent.KEYCODE_SEARCH
|| keyCode == KeyEvent.KEYCODE_MENU) {
}
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
stringBuilder.delete(0, stringBuilder.length());
finish();
}
String str = stringBuilder.toString().trim();
value.setText(str);
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以尝试这个代码:
you can try this code :
您日常工作的这一部分正在丢失数据。
char
不可能包含所有可用的 Unicode 代码点。我不知道这是否与给您带来麻烦的问题有关,但这表明您没有正确做事,并且尝试修补错误可能是一个错误 - 修复此问题将需要进行更根本的返工至少是你的事件处理程序。This portion of your routine is losing data. A
char
can't possible contain all available Unicode codepoints. I don't know if this is related to what is giving you trouble, but it is a sign that you're not doing things correctly and trying to patch over your bugs is probably a mistake -- fixing this will require a more fundamental reworking of your event handler, at least.