如何在android中制作键盘应用程序

时间:2015-01-15 07:44:49

标签: android keyboard key

我想在android中制作键盘应用程序。

为此,我尝试下面的代码,但它确实无效。

  1. 我在活动中制作button1,
  2. 我按下按钮1
  3. 然后我希望它与我在键盘上输入键时相同。
  4. 我该怎么做?请帮忙。

    public class MainActivity extends Activity {
    
    Button button1;
    EditText edit;
    
    KeyEvent keyevent1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        button1 = (Button) findViewById(R.id.button);
    
        button1.setOnClickListener(new OnClickListener(){
    
            public void onClick(View v){
    
                keyevent1 = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_ENTER);
    
    
                System.out.println("aaaa:  " + keyevent1);
    
                onKeyDown(KeyEvent.KEYCODE_ENTER, keyevent1);
    
            }
    
        });     
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    
        System.out.println("bbbb:  " + event);
    
        return super.onKeyDown(keyCode, event);
    }
    
    }
    

1 个答案:

答案 0 :(得分:0)

addCourseText = (EditText) findViewById(R.id.clEtAddCourse);
addCourseText.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
    if (event.getAction() == KeyEvent.ACTION_DOWN)
    {
        switch (keyCode)
        {
            case KeyEvent.KEYCODE_DPAD_CENTER:
            case KeyEvent.KEYCODE_ENTER:
                addCourseFromTextBox();
                return true;
            default:
                break;
        }
    }
    return false;
}});