首页 > 移动平台 > 详细

Android--OnKeyListner

时间:2015-03-05 19:35:17      阅读:261      评论:0      收藏:0      [点我收藏+]

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 {
<span style="white-space:pre">	</span>private TextView txt=null;
<span style="white-space:pre">	</span>private EditText edit=null;
<span style="white-space:pre">	</span>public void onCreate(Bundle savedInstanceState) {
<span style="white-space:pre">		</span>super.onCreate(savedInstanceState); // 生命周期方法
<span style="white-space:pre">		</span>super.setContentView(R.layout.main); // 设置要使用的布局管理器
<span style="white-space:pre">		</span>this.edit=(EditText)super.findViewById(R.id.edit);
<span style="white-space:pre">		</span>this.txt=(TextView)super.findViewById(R.id.text);
<span style="white-space:pre">		</span>this.edit.setOnKeyListener(new OnKeyListenerImpl());


<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>private class OnKeyListenerImpl implements OnKeyListener{


<span style="white-space:pre">		</span>public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
<span style="white-space:pre">			</span>String str=Hello.this.edit.getText().toString();
<span style="white-space:pre">			</span>Hello.this.txt.setText(str);
<span style="white-space:pre">			</span>return false;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}
}

运行如下:

技术分享

Android--OnKeyListner

原文:http://blog.csdn.net/yayun0516/article/details/44084903

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!