接上次的教程,这次我们在界面中加个EditText,EditText是什么?看名字就知道啦,什么?你小学英语没学好,我晕,Edit是编辑的意思,Text是文本,连一起就是可编辑文本控件.我们看一下main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/editText"> </EditText> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮" android:id="@+id/button"></Button> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/hello" android:id="@+id/text"></TextView> </LinearLayout>接着就是Activity中的代码了:
我们这次增加一点,OK,写完了,还是来运行一下,是不是输入的信息点击按钮都会显示出来,很有趣吧?什么?不能运行?那肯定是哪里写错了,你再查看查看(look,look).呵呵,写到这里你一定认为写完了,OH,NO,我们还需要再加点信息.也很简单,我们要对输入的信息做点判断,你不能什么也不输吧?总得输点东西才有看头啊!
将最底下的updateText()改一下呢,代码如下:
private void updateText() { // 取得EditText中输入的文本信息 String edit_str = edit_text.getText().toString(); if (edit_str.trim().length() == 0) { text.setText("你可什么都没输啊?想叫我显示什么呢?"); } else { // 将文本信息设置给TextView控件显示出来 text.setText(edit_str); } }
OK,好了,这篇教程就结束了,EditText控件也很简单吧,这可是很重要的控件哦,记住了!