如何在活动中使用InputMethodService?

时间:2013-02-26 15:47:55

标签: android

我有活动课

public class Login extends Activity  

我在Login类中创建了类

public  class Keyclass extends InputMethodService
    {
        void addtext()
        {
            InputConnection ic =  getCurrentInputConnection();
             ic.commitText("1", 1);
        }
    }

并尝试在onClick事件中使用它:

Button but1=(Button) findViewById(R.id.button1);
         but1.setOnClickListener(new OnClickListener() 
            {
                @Override
                public void onClick(View arg0) 
                {
                    Keyclass kk= new Keyclass();
                    kk.onkey();
                }
            });

但是得到错误:

  

02-26 21:28:18.903:E / AndroidRuntime(747):致命异常:主02-26   21:28:18.903:E / AndroidRuntime(747):java.lang.NullPointerException

1 个答案:

答案 0 :(得分:-1)

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {

        // do what you want with the power button           
        Log.i(TAG,"PowerButton got clicked!");

        return true;
    }
    return super.onKeyDown(keyCode, event);
}