【Android】鍵盤事件監聽OnKeyListener

main.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
 
    <EditText
        android:id="@+id/edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

.java代碼如下:

package org.lxh.demo;
 
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
 
public class Hello extends Activity {
private TextView txt=null;
private EditText edit=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命週期方法
super.setContentView(R.layout.main); // 設置要使用的佈局管理器
this.edit=(EditText)super.findViewById(R.id.edit);
this.txt=(TextView)super.findViewById(R.id.text);
this.edit.setOnKeyListener(new OnKeyListenerImpl());
 
 
}
private class OnKeyListenerImpl implements OnKeyListener{
 
 
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
String str=Hello.this.edit.getText().toString();
Hello.this.txt.setText(str);
return false;
 
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章