簡體   English   中英

收聽在edittext上按下的任何鍵

[英]Listen any key pressed on edittext

我有一個問題,當我按下手機上的任何鍵時如何處理EditText ,然后將顯示一個Toast及其內容:“按下鍵”。

我用了:

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

但這不行。 有誰能夠幫助我?

這是我在應用程序上的功能,當我在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();
        }
    }
});

嘗試以下-

 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();
            }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM