android 之 EditText控件
[java]
<span style="font-size:18px;">
</span>
[java] view plaincopyprint?
<span style="font-size:18px;">package zm.dmy;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;
public class EditView1Activity extends Activity {
/** Called when the activity is first created. */
private TextView TextView1 ;
private EditText EditText1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView1 = (TextView)findViewById(R.id.myTextView);
EditText1 = (EditText)findViewById(R.id.myEditText);
//设置当edittext为空时的提示内容
EditText1.setHint("请输入aaaaa内容");
//<span style="color:#ff0000;">设置edittext事件监听。。。。<span style="font-family: Arial; line-height: 26px; text-align: left; ">主程序中唯一也是关键之处,便是利用EditText.OnKeyListener来拦截EditText的键盘输入事件,仅需要在其中重写onKey()方法,在onKey()方法中,将EditText.getText()取出来的文字,显示于TextView当中。</span></span>
//new EditText.OnKeyListener() 和 new OnKeyListener()一样
EditText1.setOnKeyListener(new EditText.OnKeyListener() {
public boolean onKey(View arg0,int arg1,KeyEvent arg2)
{
TextView1.setText("文本框内容是" + EditText1.getText().toString());
return false;
}
});
}
}</span>
main.xml中有点要注意:
android:layout_x 和android:layout_y只能在绝对布局中才有效。。。
[html]
<span style="font-size:18px;"><?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" >
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
<span style="color:#ff0000;"> android:layout_x = "29px"
android:layout_y = "30px"</span>
/>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/mytext" />
</LinearLayout></span>
作者:Bill_Ming
补充:移动开发 , Android ,