以编程方式更改EditText IME_ACTION

时间:2012-09-19 11:21:50

标签: android

在我的应用程序中,我需要显示一个或两个edittexts来收集信息(e1和e2),具体取决于用户通过单选按钮进行的​​选择。这是通过将edittext的可见性状态设置为GONE来完成的,并且工作正常。

我的问题是如何以编程方式为每种情况将IME_ACTION从“完成”设置为“下一步”,即:

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 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:1)

万一有人降落在这里:EditText.setImeOptions(EditorInfo.IME_ACTION_NEXT);如果是TouchWiz,上帝帮助你:)

另外,我注意到EditText如果有焦点就无法切换时出现问题,所以一定要关闭键盘并取消焦点