歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android開發之EditText組件

輸入特定字符

效果圖:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <TableLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:stretchColumns="1" >

 

        <TableRow >

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:text="@string/user" />

 

            <EditText

                android:layout_width="wrap_content"

                android:layout_height="wrap_content" />

        </TableRow>

 

        <TableRow >

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:text="@string/pass" />

 

            <EditText

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:inputType="textPassword" />

        </TableRow>

 

        <TableRow >

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="@string/twopass" />

 

            <EditText

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:inputType="textPassword" />

        </TableRow>

 

        <TableRow >

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="@string/email" />

 

            <EditText

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:inputType="textEmailAddress" />

        </TableRow>

 

        <TableRow >

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:text="@string/authcode" />

 

            <TableLayout

                android:layout_width="wrap_content"

                android:layout_height="wrap_content" >

 

                <TableRow >

                    <EditText

                        android:layout_width="80dp"

                        android:layout_height="wrap_content" />

 

                    <ImageView

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:background="@drawable/p2" />

 

                    <TextView

                        android:id="@+id/changeId"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:text="@string/change" />

                </TableRow>

            </TableLayout>

        </TableRow>

 

        <TableRow >

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content" />

 

            <CheckBox

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:layout_centerInParent="true"

                android:text="@string/text"

                android:id="@+id/textId"

                 />

        </TableRow>

    </TableLayout>

 

    <RelativeLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent" >

 

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_centerInParent="true"

            android:text="提交注冊" />

    </RelativeLayout>

 

</LinearLayout>

////////////////strings.xml//////////////////////

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

    <string name="hello">Hello World, ZhuCeActivity!</string>

    <string name="app_name">ZhuCe</string>

    <string name="user">用戶名:</string>

    <string name="pass">密碼:</string>

    <string name="twopass">確認密碼:</string>

    <string name="email">電子郵箱:</string>

    <string name="authcode">驗證碼:</string>

    <string name="change">看不清?<a href="#">換一個</a></string>

    <string name="text">我已經閱讀並同意協議</string>

   

</resources>

自動完成輸入內容的組件

package cn.class3g.activity;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

import android.widget.MultiAutoCompleteTextView;

 

public class AutoActivity extends Activity{

   AutoCompleteTextView auto = null;

   MultiAutoCompleteTextView mauto = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        findViews();

        String[] str = {"abc","add","aas","afr","bdr","ber","bsd"};

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line,str);

       

        auto.setAdapter(adapter);

       

        //----------------------

        mauto.setAdapter(adapter);

        mauto.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

       

    }

    private void findViews(){

      auto = (AutoCompleteTextView) this.findViewById(R.id.autoId);

      mauto = (MultiAutoCompleteTextView) this.findViewById(R.id.mautoId);

    }

}

EditText中回車鍵的使用

為EditText對象的注冊OnKeyListener事件,實現onKey()方法

package cn.class3g.activity;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.View;

import android.view.View.OnKeyListener;

import android.widget.Button;

import android.widget.EditText;

 

public class EditTextTest1Activity extends Activity implements OnKeyListener{

    /** Called when the activity is first created. */

   Button but = null;

   EditText et = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.onkey);

        findView();

    }

    public void findView(){

      but = (Button) this.findViewById(R.id.buttonId);

      et = (EditText) this.findViewById(R.id.edit);

      et.setOnKeyListener(this);

    }

 

   @Override

   public boolean onKey(View v, int keyCode, KeyEvent event) {

      // TODO Auto-generated method stub

      if(keyCode==KeyEvent.KEYCODE_ENTER){

        but.setText(et.getText());

        et.setVisibility(View.GONE);

        but.setVisibility(View.VISIBLE);

      }

      return false;

   }

}

Copyright © Linux教程網 All Rights Reserved