以编程方式更改EditText的IME_ACTION

8

在我的应用程序中,我需要显示一个或两个编辑框来收集信息(e1和e2),这取决于用户通过单选按钮进行的选择。这是通过将编辑框的可见性状态设置为“GONE”来完成的,并且运行良好。

我的问题是如何以编程方式将IME_ACTION从“done”设置为“next”,即:

1)只有e1可见 - 将e1的IME_ACTION设置为DONE

2)e1和e2都可见 - 将e1的IME_ACTION设置为NEXT,将e2的IME_ACTION设置为DONE。

我正在使用android:minSdkVersion="4"和android:targetSdkVersion="16",并在Android 2.2设备上进行测试。

这是我的布局:

<EditText
android:id="@+id/e1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="@string/sh15"
android:textColor="@android:color/black"
android:textSize="@dimen/s">
</EditText>         
<EditText
android:id="@+id/e2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="@string/sh16"
android:textColor="@android:color/black"
android:textSize="@dimen/s">
</EditText> 

这是我的代码:

这是我的代码:

RadioGroup r= (RadioGroup) dialog.findViewById(R.id.rg);
  r.setOnCheckedChangeListener(new OnCheckedChangeListener() 
  {
   public void onCheckedChanged(RadioGroup group, int checkedId) 
   {
    switch(checkedId)
    {
     case R.id.rb1: //show one edittext
         e1.setVisibility(View.VISIBLE);             
         e2.setVisibility(View.GONE);
         e1.setImeOptions(EditorInfo.IME_ACTION_DONE);
     break;
     case R.id.rb2: //show two edittext
         e1.setVisibility(View.VISIBLE);
         e2.setVisibility(View.VISIBLE);
         e1.setImeOptions(EditorInfo.IME_ACTION_NEXT);
         e2.setImeOptions(EditorInfo.IME_ACTION_DONE);
     break;

    }
   }
  });  
2个回答

6
e2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Overrid
    public boolean onEditorAction(TextView v, int actionId,KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
                //your code    
              }
          }
)};

对不起,我不理解你的想法。我所需要的是一种方法,根据可见的编辑文本数量,从IME_ACTION_DONE更改为IME_ACTION_NEXT。 - Pedro

1
如果有人误打误撞地来到这里:EditText.setImeOptions(EditorInfo.IME_ACTION_NEXT); 如果是TouchWiz的话,上帝保佑你 :)
另外,我注意到如果EditText在焦点状态下无法切换,就会出现问题,所以一定要关闭键盘并取消焦点。

@Sudhasri 你正在测试哪些设备规格? - AllDayAmazing
因此,注意关于_TouchWiz_ :) - 还要检查确保您只有单行EditText,并且在尝试更改它时,您的EditText没有焦点。 - AllDayAmazing
1
对我有效的方法是添加 android:singleLine="true",我没有提及任何 imeOptions 或 next focus 值。我得到了预期的结果。在第一个 editText 中,如果另一个 editText 可见,则键盘会显示下一个,否则它会显示完成 :-p - Sudhasri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接