Android EditText学习

上一篇文章介绍了EditText的属性,这里介绍EditText的使用方法。

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

	<EditText 
	    android:id="@+id/editText1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="这是一个EditText"
    />
    

</RelativeLayout>

 运行效果如下

如果要在EditText中使用说明性文字,如:请输入帐号等。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

	<EditText 
	    android:id="@+id/editText1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:maxLength="10"
	    android:hint="请输入帐号"
    />
    

</RelativeLayout>

 运行如下

当然,我也可以改变颜色

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

	<EditText 
	    android:id="@+id/editText1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:maxLength="10"
	    android:textColorHint="#ff00ff"
	    android:hint="请输入帐号"
    />
    

</RelativeLayout>

 运行效果如下

实现Html中Textarea文本域,在Android中没有专用的文本域控件,但是我们可以通过设置EditText的属性来实现

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

	<EditText 
	    android:id="@+id/editText1"
	    android:layout_width="match_parent"
	    android:layout_height="300dp"
		android:gravity="top"
    />
    

</RelativeLayout>

 运行如下

实现输入密码框

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

	<EditText 
	    android:id="@+id/editText1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
         android:password="true"	    />

</RelativeLayout>

 

实现只输入数字

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

	<EditText 
	    android:id="@+id/editText1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
		android:inputType="number"
    />

</RelativeLayout>

 运行效果

只能输入电话号码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

	<EditText 
	    android:id="@+id/editText1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
		android:phoneNumber="true"
    />

</RelativeLayout>

 

在EditText中显示图片

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

	<EditText 
	    android:id="@+id/editText1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:drawableLeft="@drawable/ic_launcher"
    />

</RelativeLayout>

 运行效果

实现圆角 EditText,要实现圆角EditText,首先要写一个shape文件,在drawable下面新建一个edit_border.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#aea594" />
      <corners android:topLeftRadius="5dp"    
        android:topRightRadius="5dp"     
        android:bottomRightRadius="5dp"    
        android:bottomLeftRadius="5dp"/>
</shape>

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

	<EditText 
	    android:id="@+id/editText1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:background="@layout/edit_border"
	    android:layout_margin="10dp"
	    android:text="圆角EditText。。。"
    />

</RelativeLayout>

 运行效果

下面看EditText的一个示例

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

	<EditText 
	    android:id="@+id/editText1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:background="@layout/edit_border"
	    android:layout_margin="10dp"
    />

	<Button 
	    android:id="@+id/button1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@id/editText1"
	    android:text="全选"
	    
	    />
	<Button 
	    android:id="@+id/button2"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@id/button1"
	    android:text="EditText的值"
	    />
	<Button 
	    android:id="@+id/button3"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@id/button2"
	    android:text="第3个到最后"
	    />
	<Button 
	    android:id="@+id/button4"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@id/button3"
	    android:text="获取选择的内容"
	    />
</RelativeLayout>

 java代码

package com.zhoucj.edittextdemo;

import java.net.Inet4Address;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.Selection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity
{

    EditText editText;
    Button button1;
    Button button2;
    Button button3;
    Button button4;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        editText=(EditText)findViewById(R.id.editText1);
        button1=(Button)findViewById(R.id.button1);
        button1.setOnClickListener(new button1ClickListener());
        button2=(Button)findViewById(R.id.button2);
        button2.setOnClickListener(new button2ClickListener());
        button3=(Button)findViewById(R.id.button3);
        button3.setOnClickListener(new button3ClickListener());
        button4=(Button)findViewById(R.id.button4);
        button4.setOnClickListener(new button4ClickListener());
    }
    
    class button1ClickListener implements OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            editText.selectAll();
        }
    }
    
    class button2ClickListener implements OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            Toast.makeText(MainActivity.this, editText.getText().toString(), Toast.LENGTH_LONG).show();
        }
    }
    
    class button3ClickListener implements OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            Editable edtext=editText.getText();
            Selection.setSelection(edtext, 2, edtext.length());
        }
    }
    
    class button4ClickListener implements OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            int s=editText.getSelectionStart();
            int e=editText.getSelectionEnd();
            String text=editText.getText().subSequence(s, e).toString();
            
            Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

运行效果

 

这里介绍了EditText常用功能。想学习的朋友可以试试EditText的其它应用。

 

 

posted @ 2013-06-19 11:53  最後的輕語  阅读(367)  评论(0编辑  收藏  举报