将键盘从qwerty切换到数字键盘,反之亦然

时间:2014-11-08 09:24:47

标签: android android-edittext android-keypad

我想调整editText输入类型,这样我首先想要在keybord上显示数字,用一个按钮将keyborad从数字键盘切换到qwerty。我发现很多类似的问题与此相关但没有任何结果。

我试图通过设置ime动作按钮来做到这一点。不知道它是否正确但它对我不起作用。还有其他替代方案吗?

以下是代码:

的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.helloword.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText android:id="@+id/ed"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:imeOptions="actionGo|actionNext|actionSend|actionDone"
        android:imeActionLabel="abc"
        android:inputType="number"
        android:imeActionId="@+id/my_ime"/>

</RelativeLayout>

的活动:

public class MainActivity extends ActionBarActivity {
EditText ed;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed = (EditText)findViewById(R.id.ed);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        ed.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                // TODO Auto-generated method stub

                if(arg1==R.id.my_ime)
                {
                    Log.e("inside", "inside");
                    ed.setInputType(InputType.TYPE_CLASS_TEXT);

                }
                return false;
            }
        });
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

0 个答案:

没有答案