他们以任何方式在Android中获得键盘的点击事件吗?实际上我正在开发一个应用程序,需要键盘的点击事件。例如,如果用户在Android手机中按键盘上的字母“a”,那么我想要点击'a'事件。最近我遇到了onKeyListener并使用了各种KeyCode,但最终它并没有帮助我。
public class KeyCode {
private Context con;
private EditText edt;
private ArrayList<String> array;
public KeyCode(Context con, final ArrayList<String> array, final EditText edt)
{
// TODO Auto-generated constructor stub
this.array=array;
this.con=con;
this.edt=edt;
edt.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode==event.KEYCODE_A)
{
if(event.isCapsLockOn())
{
edt.append(array.get(0));
}
else
{
edt.append(array.get(26));
}
}
if(keyCode==event.KEYCODE_B)
{
if(event.isCapsLockOn())
{
edt.append(array.get(1));
}
else
{
edt.append(array.get(27));
}
}
if(keyCode==event.KEYCODE_C)
{
if(event.isCapsLockOn())
{
edt.append(array.get(2));
}
else
{
edt.append(array.get(28));
}
}
if(keyCode==event.KEYCODE_D)
{
if(event.isCapsLockOn())
{
edt.append(array.get(3));
}
else
{
edt.append(array.get(29));
}
}
if(keyCode==event.KEYCODE_E)
{
if(event.isCapsLockOn())
{
edt.append(array.get(4));
}
else
{
edt.append(array.get(30));
}
}
if(keyCode==event.KEYCODE_F)
{
if(event.isCapsLockOn())
{
edt.append(array.get(5));
}
else
{
edt.append(array.get(31));
}
}
if(keyCode==event.KEYCODE_G)
{
if(event.isCapsLockOn())
{
edt.append(array.get(6));
}
else
{
edt.append(array.get(32));
}
}
if(keyCode==event.KEYCODE_H)
{
if(event.isCapsLockOn())
{
edt.append(array.get(7));
}
else
{
edt.append(array.get(33));
}
}
if(keyCode==event.KEYCODE_I)
{
if(event.isCapsLockOn())
{
edt.append(array.get(8));
}
else
{
edt.append(array.get(34));
}
}
if(keyCode==event.KEYCODE_J)
{
if(event.isCapsLockOn())
{
edt.append(array.get(9));
}
else
{
edt.append(array.get(35));
}
}
if(keyCode==event.KEYCODE_K)
{
if(event.isCapsLockOn())
{
edt.append(array.get(10));
}
else
{
edt.append(array.get(36));
}
}
if(keyCode==event.KEYCODE_L)
{
if(event.isCapsLockOn())
{
edt.append(array.get(11));
}
else
{
edt.append(array.get(37));
}
}
if(keyCode==event.KEYCODE_M)
{
if(event.isCapsLockOn())
{
edt.append(array.get(12));
}
else
{
edt.append(array.get(38));
}
}
if(keyCode==event.KEYCODE_N)
{
if(event.isCapsLockOn())
{
edt.append(array.get(13));
}
else
{
edt.append(array.get(39));
}
}
if(keyCode==event.KEYCODE_O)
{
if(event.isCapsLockOn())
{
edt.append(array.get(14));
}
else
{
edt.append(array.get(40));
}
}
if(keyCode==event.KEYCODE_P)
{
if(event.isCapsLockOn())
{
edt.append(array.get(15));
}
else
{
edt.append(array.get(41));
}
}
if(keyCode==event.KEYCODE_Q)
{
if(event.isCapsLockOn())
{
edt.append(array.get(16));
}
else
{
edt.append(array.get(42));
}
}
if(keyCode==event.KEYCODE_R)
{
if(event.isCapsLockOn())
{
edt.append(array.get(17));
}
else
{
edt.append(array.get(43));
}
}
if(keyCode==event.KEYCODE_S)
{
if(event.isCapsLockOn())
{
edt.append(array.get(18));
}
else
{
edt.append(array.get(44));
}
}
if(keyCode==event.KEYCODE_T)
{
if(event.isCapsLockOn())
{
edt.append(array.get(19));
}
else
{
edt.append(array.get(45));
}
}
if(keyCode==event.KEYCODE_U)
{
if(event.isCapsLockOn())
{
edt.append(array.get(20));
}
else
{
edt.append(array.get(46));
}
}
if(keyCode==event.KEYCODE_V)
{
if(event.isCapsLockOn())
{
edt.append(array.get(21));
}
else
{
edt.append(array.get(47));
}
}
if(keyCode==event.KEYCODE_W)
{
if(event.isCapsLockOn())
{
edt.append(array.get(22));
}
else
{
edt.append(array.get(48));
}
}
if(keyCode==event.KEYCODE_X)
{
if(event.isCapsLockOn())
{
edt.append(array.get(23));
}
else
{
edt.append(array.get(49));
}
}
if(keyCode==event.KEYCODE_Y)
{
if(event.isCapsLockOn())
{
edt.append(array.get(24));
}
else
{
edt.append(array.get(50));
}
}
if(keyCode==event.KEYCODE_Z)
{
if(event.isCapsLockOn())
{
edt.append(array.get(25));
}
else
{
edt.append(array.get(51));
}
}
if(keyCode==event.KEYCODE_0)
{
edt.append(array.get(52));
}
if(keyCode==event.KEYCODE_1)
{
edt.append(array.get(53));
}
if(keyCode==event.KEYCODE_2)
{
edt.append(array.get(54));
}
if(keyCode==event.KEYCODE_3)
{
edt.append(array.get(55));
}
if(keyCode==event.KEYCODE_4)
{
edt.append(array.get(56));
}
if(keyCode==event.KEYCODE_5)
{
edt.append(array.get(57));
}
if(keyCode==event.KEYCODE_6)
{
edt.append(array.get(58));
}
if(keyCode==event.KEYCODE_7)
{
edt.append(array.get(59));
}
if(keyCode==event.KEYCODE_8)
{
edt.append(array.get(60));
}
if(keyCode==event.KEYCODE_9)
{
edt.append(array.get(61));
}
/*if(keyCode==event.KEYCODE_DEL)
{
edt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
edt.setText(s);
}
});
}*/
/*if(keyCode==event.KEYCODE_SPACE)
{
edt.append(" ");
}*/
if(keyCode==event.KEYCODE_COMMA)
{
edt.append(",");
}
if(keyCode==event.KEYCODE_PERIOD)
{
edt.append(".");
}
if(keyCode==event.KEYCODE_EQUALS)
{
edt.append("=");
}
if(keyCode==event.KEYCODE_LEFT_BRACKET)
{
edt.append("(");
}
if(keyCode==event.KEYCODE_RIGHT_BRACKET)
{
edt.append(")");
}
if(keyCode==event.KEYCODE_SEMICOLON)
{
edt.append(";");
}
if(keyCode==event.KEYCODE_APOSTROPHE)
{
edt.append("'");
}
if(keyCode==event.KEYCODE_GRAVE)
{
edt.append("~");
}
if(keyCode==event.KEYCODE_SLASH)
{
edt.append("/");
}
if(keyCode==event.KEYCODE_NUMPAD_DIVIDE)
{
edt.append("/");
}
if(keyCode==event.KEYCODE_NUMPAD_MULTIPLY)
{
edt.append("*");
}
if(keyCode==event.KEYCODE_NUMPAD_SUBTRACT)
{
edt.append("-");
}
if(keyCode==event.KEYCODE_NUMPAD_ADD)
{
edt.append("+");
}
return true;
}
});
}
}
这是我的java类。我有一些特定字符的数组,我将它传递给这个java文件,用数组中包含的字符替换默认字符。字符被替换但是当从笔记本电脑键盘按下键而不是从Android手机的键盘按下时。
public class ChangeFont extends ActionBarActivity {
private EditText edt;
private ImageView img1,img2,img3,img4,img5;
private ListView lst, savelist;
private TextView txtNew,txtSave;
private DataBaseHelper data;
private ArrayList<String> category;
private ArrayList<String> styleArray;
private Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_font);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setTitle("Fonts");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
data=new DataBaseHelper(this);
edt=(EditText)findViewById(R.id.edit_text);
img1=(ImageView)findViewById(R.id.image1);
img2=(ImageView)findViewById(R.id.image2);
img3=(ImageView)findViewById(R.id.image3);
img4=(ImageView)findViewById(R.id.image4);
img5=(ImageView)findViewById(R.id.image5);
lst=(ListView)findViewById(R.id.list);
savelist=(ListView)findViewById(R.id.savelist);
txtNew=(TextView)findViewById(R.id.textnew);
txtSave=(TextView)findViewById(R.id.textsave);
category=new ArrayList<String>();
styleArray=new ArrayList<String>();
try {
data.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c=data.getSample();
if (c.moveToFirst())
{
while(!c.isAfterLast())
{
String cat = c.getString(c.getColumnIndex(DataBaseUtil.SAMPLE));
category.add(cat);
c.moveToNext();
}
}
/*l=data.getAllLockType();
if (l.moveToFirst())
{
while(!l.isAfterLast())
{
String cat=l.getString(l.getColumnIndex(DataBaseUtil.LOCK_TYPE));
Log.i("String",""+cat);
locktype.add(cat);
l.moveToNext();
}
}
Log.i("Lock Array",""+locktype);*/
SampleStyleAdapter aa=new SampleStyleAdapter(getApplicationContext(), category);
lst.setAdapter(aa);
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
data.openDataBase();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt, InputMethodManager.SHOW_IMPLICIT);
final String stylename = category.get(position).toString();
Toast.makeText(getApplicationContext(), "" + stylename + "position" + position, Toast.LENGTH_SHORT).show();
List<DataBaseModel> sy = data.getStyle(position);
styleArray = new ArrayList<String>();
for (DataBaseModel model : sy) {
String vimal = model.getStyle();
styleArray.add(vimal);
}
Log.i("Style Array", "" + styleArray);
KeyCode key=new KeyCode(getApplicationContext(),styleArray,edt);
//Log.i("Called Key Code","Class");
}
});
edt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lst.setVisibility(View.GONE);
savelist.setVisibility(View.GONE);
txtNew.setVisibility(View.VISIBLE);
txtSave.setVisibility(View.VISIBLE);
edt.requestFocus();
edt.setCursorVisible(true);
//InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//imm.showSoftInput(edt, InputMethodManager.SHOW_IMPLICIT);
}
});
img1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
lst.setVisibility(View.VISIBLE);
savelist.setVisibility(View.GONE);
txtNew.setVisibility(View.GONE);
txtSave.setVisibility(View.GONE);
edt.setCursorVisible(false);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edt.getWindowToken(), 0);
}
});
img2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
lst.setVisibility(View.GONE);
txtNew.setVisibility(View.VISIBLE);
txtSave.setVisibility(View.VISIBLE);
edt.requestFocus();
edt.setCursorVisible(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt, InputMethodManager.SHOW_IMPLICIT);
}
});
img3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(ChangeFont.this, EmojiTabbed.class);
startActivity(i);
}
});
img4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lst.setVisibility(View.GONE);
savelist.setVisibility(View.VISIBLE);
txtNew.setVisibility(View.GONE);
txtSave.setVisibility(View.GONE);
edt.setCursorVisible(false);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edt.getWindowToken(), 0);
}
});
img5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(ChangeFont.this, Share.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_change_font, menu);
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
Intent i=new Intent(this, Home.class);
NavUtils.navigateUpTo(this, i);
return true;
}
return super.onOptionsItemSelected(item);
}
}
这件事是否可能?
答案 0 :(得分:1)
点击事件发生鼠标点击或触地,你需要捕捉像这样的按键事件
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (i == KeyEvent.KEYCODE_BACK) {
//back button key up
}
return super.onKeyUp(keyCode, event);
}
对于点击事件,您应该使用此
yourControl.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// your code
}
});
我跳这会帮助你
答案 1 :(得分:0)
您可以通过键码获取点击的关键字:
List<Key> keys = getKeyboard().getKeys();
for (Key key : keys) {
if (key.codes[0] == 65) { //A
} }
供参考 http://www.fampennings.nl/maarten/android/09keyboard/index.htm