English 中文(简体)
编辑文本上的 GetFocus () 的奇怪行为
原标题:Strange behaviour of getFocus() on EditTexts

所以我有3个编辑文本框, 当用户按下其中的一个输入时, 我想继续下一个。

我试过了:

final EditText email = (EditText) findViewById(R.id.editText1);
email.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {

            findViewById(R.id.editText2).requestFocus();
          return true;
        }
        return false;
    }
});


final EditText pass = (EditText) findViewById(R.id.editText2);
pass.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {

            findViewById(R.id.editText3).requestFocus();
          return true;
        }
        return false;
    }
});



final EditText passrep = (EditText) findViewById(R.id.editText3);
passrep.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {

            findViewById(R.id.button1).requestFocus();
          return true;
        }
        return false;
    }
});

但使用此代码, 焦点会从编辑Text1跳到键盘输入的编辑Text3 。

这个工作(我偶然发现)是什么呢?

final EditText email = (EditText) findViewById(R.id.editText1);
email.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {

            findViewById(R.id.editText2).requestFocus();
          return true;
        }
        return false;
    }
});


final EditText pass = (EditText) findViewById(R.id.editText2);
email.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {

            findViewById(R.id.editText3).requestFocus();
          return true;
        }
        return false;
    }
});



final EditText passrep = (EditText) findViewById(R.id.editText3);
email.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {

            findViewById(R.id.button1).requestFocus();
          return true;
        }
        return false;
    }
});

例如,为同一份《编辑图》(电子邮件)设置三个不同的OnKeyListeners。

我错过什么了 这是我的布局

<EditText
    android:id="@+id/editText1"
    android:layout_width="234dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:ems="10"
    android:inputType="textEmailAddress"
    android:layout_marginTop="20dp"
    android:hint="Email" >

</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="234dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:ems="10"
    android:inputType="textPassword"
    android:layout_marginTop="20dp"
    android:hint="Password" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="234dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:ems="10"
    android:inputType="textPassword"
    android:layout_marginTop="20dp"
    android:hint="Confirm Password" />

<Button
    android:id="@+id/button1"
    android:layout_width="123dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="35dp"
    android:text="@string/submit" />
最佳回答

实际上,这里发生的一切就是 你们所有的 EditText 正在收听关键事件 < nuger> all the time 是否在焦点中

您需要为 Done/ Enter/ Go/ Search 密钥使用 < code> on Editor Action Liistener

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //do something
        }
    return false;
    }
});
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签