通过软键盘输入进行点击

时间:2013-02-07 12:12:54

标签: android interface android-softkeyboard enter

嗯..也许是一个有点愚蠢的问题,但是我不知道我自己......

    package com.example.wettkampftimerbt;
    import android.content.Intent;
    import android.inputmethodservice.Keyboard.Key;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;

    public class ServMPW extends Activity_ActivityGroup implements OnClickListener{

        Button enter, cancel;
        EditText eingabe, eingabe2, masterpw;
        String pwx, eingabex, pass, adminacchint1x, adminacchint2x;
    private final String PREFS_NAME = "prefs";  
    private final String PREFS_PW = "pw";
    private final String PREFS_lang = "lang";
    private final String TITLE5 = "title5";
    private final String ADMINACCHINT1 = "adminacchint1";
    private final String ADMINACCHINT2 = "adminacchint2";
        @Override
        public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
     setContentView(R.layout.servmpw);   
     //((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
     String title5x=(getSharedPreferences(PREFS_lang, MODE_PRIVATE).getString(TITLE5, "Administrationszugang"));
    this.setTitle(title5x);
     adminacchint1x=(getSharedPreferences(PREFS_lang, MODE_PRIVATE).getString(ADMINACCHINT1, "Passwort"));
     adminacchint2x=(getSharedPreferences(PREFS_lang, MODE_PRIVATE).getString(ADMINACCHINT2, "falsches Passwort"));

            enter=(Button)findViewById(R.id.enter);
            cancel=(Button)findViewById(R.id.cancel);
            eingabe=(EditText)findViewById(R.id.eingabe);
            masterpw=(EditText)findViewById(R.id.masterpw);
            eingabe.setHint(adminacchint1x);
            eingabe2=(EditText)findViewById(R.id.eingabe2);
            enter.setOnClickListener(this);
            cancel.setOnClickListener(this);

    }
            public void onClick (View v){

             if (v==cancel){
                    finish();}
            else if (v == enter){
                entermeth();
            }   

                    else {
                eingabe.setText("");
                eingabe.setHint(adminacchint2x);}}



public void OnKey (View v, int keyCode, KeyEvent event) 
{
    if (keyCode==66)
        entermeth();
}
public void entermeth(){

    eingabe2.setText(getSharedPreferences(PREFS_NAME, MODE_PRIVATE).getString(PREFS_PW, "bbb"));  
    pwx = eingabe2.getText().toString();
    Log.e("pw", PREFS_PW + "." + pwx);
    eingabex = eingabe.getText().toString(); 
    masterpw.setText("aaa");
    String masterpwx = masterpw.getText().toString();
    Log.e("pw:", masterpwx);
    if (eingabex.equals(pwx)||(eingabex.equals(masterpwx))){
        Intent admin = new Intent (ServMPW.this, Admin.class);
        ServMPW.this.finish();
        startActivity(admin);}


}
}

如果按下软键盘上的enter按钮,我想在我自己的enterbutton上执行单击。但它不起作用......我错过了什么?

2 个答案:

答案 0 :(得分:2)

EditText上,您可以绑定OnEditorActionListener。像这样的东西

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
       if (actionId == EditorInfo.IME_ACTION_DONE) {
           //do here your stuff here
           return true;
       }
       return false;
   }
});

答案 1 :(得分:1)

什么都没有调用OnKey方法。这就是你错过的。 此外,强制点击也不是一个好的编程习惯。您应该创建一个方法private doEnterActions(){ },然后在输入键和输入按钮上,只需调用该方法。

编辑:

就像你将setClickListener设置为你的按钮(由活动实现),你必须使活动实现,并设置为关键事件的监听器。这是使用enter.setOnKeyListener()

完成的