听取在edittext上按下的任何键

时间:2014-04-21 06:53:56

标签: java android

我有一个问题,当我从移动设备按任意键时如何处理EditText,然后会出现Toast及其内容:“按下键”。

我用过:

edittext.setOnKeyListener(new OnKeyListener() {         
        public boolean onKey(View v, int keyCode, KeyEvent event) {
        while(event.getCharacters()!=null )
                ........
        }
});

但不行。 有人能帮助我吗?

2 个答案:

答案 0 :(得分:0)

这是我在我的应用程序中的功能,当我在edittext字段中输入文本并输入时,Toast将显示输入的内容:

private Bitmap ProcessingBitmap(){
    Bitmap bm1 = null;
    Bitmap newBitmap = null;

    try {
        bm1 = BitmapFactory.decodeStream(
                 getContentResolver().openInputStream(selectedImage));

        Config config = bm1.getConfig();
        if(config == null){
            config = Bitmap.Config.ARGB_8888;
        }

        newBitmap = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(), config);
        Canvas newCanvas = new Canvas(newBitmap);

        newCanvas.drawBitmap(bm1, 0, 0, null);

        String captionString = editTextCaption.getText().toString();
        editTextCaption.getText().clear();
        if(captionString != null){

            Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
            paintText.setColor(Color.GREEN);
            paintText.setTextSize(50);
            paintText.setStyle(Style.FILL);
            paintText.setShadowLayer(10f, 10f, 10f, Color.BLACK);

            Rect rectText = new Rect();
            paintText.getTextBounds(captionString, 0, captionString.length(), rectText);

            newCanvas.drawText(captionString, 0, rectText.height(), paintText);

            Toast.makeText(getApplicationContext(), "drawText: " + captionString, Toast.LENGTH_LONG).show();

        }else{
            Toast.makeText(getApplicationContext(), "caption empty!", Toast.LENGTH_LONG).show();
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return newBitmap;
}

或者以这种方式使用它希望它有所帮助:

edittext.setOnKeyListener(new OnKeyListener() {         
    public boolean onKey(View v, int keyCode, KeyEvent event) {
    while(event.getCharacters()!=null )
            String captionString = edittext.getText().toString();
Toast.makeText(getApplicationContext(), "drawText: " + captionString, Toast.LENGTH_LONG).show();
    }
});

我把评论但是它没有显示,我不知道为什么,好吧,试试这个希望它有用,它也能在我这边工作。

edittext.setOnKeyListener(new OnKeyListener() {         
    public boolean onKey(View v, int keyCode, KeyEvent event) {
    while(event.getCharacters()!=null )
            String captionString = edittext.getText().toString();
if(captionString != null){
Toast.makeText(getApplicationContext(), "drawText: " + captionString,   Toast.LENGTH_LONG).show();
}else{
            Toast.makeText(getApplicationContext(), "caption empty!", Toast.LENGTH_LONG).show();
        }
    }
});

答案 1 :(得分:0)

尝试以下---

 edittext.setOnKeyListener(new EditText.OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if(event.getAction()==KeyEvent.ACTION_DOWN)
            {
                   //Do anything with editTextContent
                   String editTextContent = edittext.getText().toString();

                   Toast.makeText(getApplicationContext(), 
                   editTextContent +": Key is pressed", Toast.LENGTH_LONG).show();
            }
    });
相关问题