单击按钮打开键盘

发布于 2024-12-14 22:40:46 字数 2959 浏览 0 评论 0原文

我确实在单击按钮时打开键盘,并将所有按键值存储在字符串中,但我遇到了这样的问题

1. when i press CAPITAL keys then i will got 2 times char
2. when i press search and Menu button then also enter the key
3. some char i can't get like PIE

,所以请检查此代码并给我正确的答案。 我的代码是:

package com.indianic.phykeyboard;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class KeyboardActivity extends Activity {
    EditText mEdit;
    Boolean key = false;

    Button mKey, mMon;
    String getvalue;
    InputMethodManager imm;
    StringBuilder stringBuilder;
    TextView value;
    String ch = "";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mKey = (Button) findViewById(R.id.openkey);

        value = (TextView) findViewById(R.id.value);

        mKey.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, 0);
                stringBuilder = new StringBuilder();
            }
        });

    }

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_ENTER:
            return true;
        }
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.v("log_tag", "char : " + event.getUnicodeChar());

        if (event.getUnicodeChar() != 0) {
            int i = event.getUnicodeChar();
            ch = new Character((char) i).toString();
        }

        if (ch.length() > 0) {
            if (keyCode == KeyEvent.KEYCODE_DEL && stringBuilder.length() >= 1) {
                stringBuilder.delete(stringBuilder.length() - 1, stringBuilder
                        .length());
            } else if (keyCode != KeyEvent.KEYCODE_DEL
                    && keyCode != KeyEvent.KEYCODE_ENTER) {
                stringBuilder.append(ch);
            } else if (keyCode == KeyEvent.KEYCODE_SEARCH
                    || keyCode == KeyEvent.KEYCODE_MENU) {

            }
        }

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            stringBuilder.delete(0, stringBuilder.length());
            finish();
        }

        String str = stringBuilder.toString().trim();
        value.setText(str);

        return false;
    }
}

I do open Keyboard on Button click and i store all keypress value in String but i am facing some problem like this

1. when i press CAPITAL keys then i will got 2 times char
2. when i press search and Menu button then also enter the key
3. some char i can't get like PIE

So please check this code and give me proper answer.
My Code is :

package com.indianic.phykeyboard;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class KeyboardActivity extends Activity {
    EditText mEdit;
    Boolean key = false;

    Button mKey, mMon;
    String getvalue;
    InputMethodManager imm;
    StringBuilder stringBuilder;
    TextView value;
    String ch = "";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mKey = (Button) findViewById(R.id.openkey);

        value = (TextView) findViewById(R.id.value);

        mKey.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, 0);
                stringBuilder = new StringBuilder();
            }
        });

    }

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_ENTER:
            return true;
        }
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.v("log_tag", "char : " + event.getUnicodeChar());

        if (event.getUnicodeChar() != 0) {
            int i = event.getUnicodeChar();
            ch = new Character((char) i).toString();
        }

        if (ch.length() > 0) {
            if (keyCode == KeyEvent.KEYCODE_DEL && stringBuilder.length() >= 1) {
                stringBuilder.delete(stringBuilder.length() - 1, stringBuilder
                        .length());
            } else if (keyCode != KeyEvent.KEYCODE_DEL
                    && keyCode != KeyEvent.KEYCODE_ENTER) {
                stringBuilder.append(ch);
            } else if (keyCode == KeyEvent.KEYCODE_SEARCH
                    || keyCode == KeyEvent.KEYCODE_MENU) {

            }
        }

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            stringBuilder.delete(0, stringBuilder.length());
            finish();
        }

        String str = stringBuilder.toString().trim();
        value.setText(str);

        return false;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月依秋水 2024-12-21 22:40:46

你可以尝试这个代码:

public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.v("log_tag", "char : " + event.getUnicodeChar());

        if (event.getUnicodeChar() != 0) {
            int i = event.getUnicodeChar();
            ch += new Character((char) i).toString();
        }
        if (keyCode == KeyEvent.KEYCODE_DEL && ch.length() > 0) {
            ch = ch.substring(0, ch.length() - 1);
        }
        value.setText(ch);
        return super.onKeyDown(keyCode, event);
    }

you can try this code :

public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.v("log_tag", "char : " + event.getUnicodeChar());

        if (event.getUnicodeChar() != 0) {
            int i = event.getUnicodeChar();
            ch += new Character((char) i).toString();
        }
        if (keyCode == KeyEvent.KEYCODE_DEL && ch.length() > 0) {
            ch = ch.substring(0, ch.length() - 1);
        }
        value.setText(ch);
        return super.onKeyDown(keyCode, event);
    }
韬韬不绝 2024-12-21 22:40:46
    if (event.getUnicodeChar() != 0) {
        int i = event.getUnicodeChar();
        ch = new Character((char) i).toString();
    }

您日常工作的这一部分正在丢失数据。 char 不可能包含所有可用的 Unicode 代码点。我不知道这是否与给您带来麻烦的问题有关,但这表明您没有正确做事,并且尝试修补错误可能是一个错误 - 修复此问题将需要进行更根本的返工至少是你的事件处理程序。

    if (event.getUnicodeChar() != 0) {
        int i = event.getUnicodeChar();
        ch = new Character((char) i).toString();
    }

This portion of your routine is losing data. A char can't possible contain all available Unicode codepoints. I don't know if this is related to what is giving you trouble, but it is a sign that you're not doing things correctly and trying to patch over your bugs is probably a mistake -- fixing this will require a more fundamental reworking of your event handler, at least.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文