频道栏目
首页 > 资讯 > Android > 正文

Android[初级教程]第二篇 EditText控件

11-10-03        来源:[db:作者]  
收藏   我要投稿

接上次的教程,这次我们在界面中加个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中的代码了:

 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
 
public class ButtonDemoActivity extends Activity implements OnClickListener 

    private TextView text = null; 
    private EditText edit_text = null; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        // 通过ID查找到main.xml中的TextView控件 
        text = (TextView) findViewById(R.id.text); 
         
        // 通过ID查找到main.xml中的EditText控件 
        edit_text  = (EditText)findViewById(R.id.editText); 
 
        // 通过ID查找到main.xml中的Button控件 
        Button button = (Button) findViewById(R.id.button); 
        // 为Button控件增加单击监听器 
        button.setOnClickListener(this); 
 
    } 
 
    /**
     * 对main.xml中所有控件进行单击监听,当然您必须要对控件进行监听注册 
     * 例:button.setOnClickListener(this);
     */ www.2cto.com
    @Override 
    public void onClick(View v) 
    { 
        updateText(); 
 
    } 
 
    private void updateText() 
    { 
        //取得EditText中输入的文本信息 
        String edit_str = edit_text.getText().toString(); 
        //将文本信息设置给TextView控件显示出来 
        text.setText(edit_str); 
         
    } 
 
     

我们这次增加一点,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控件也很简单吧,这可是很重要的控件哦,记住了

 

摘自:kangkangz4的专栏

相关TAG标签
上一篇: Android[初级教程]第三篇 RadioButton和CheckBox控件
下一篇:Android[初级教程]第一篇 Button控件和TextView控件
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站