安卓基本控件之EditText

佈局文件

<LinearLayout 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"
    android:orientation="vertical" >

    <!--
      android:hint="請輸入用戶名。。"  提示信息
      android:digits="abcderf123" 輸入內容的限制
      android:textColorHint="#0000ff"  提示信息的顏色
      android:textColor="#ff0000"    文本顏色
      android:textCursorDrawable="@null"  文本光標  文本是什麼顏色  光標也是什麼顏色
      
       android:inputType="textPassword"  鍵盤的顯示
       
        <requestFocus/>  獲取焦點
    -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="用戶名稱:" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_toRightOf="@id/tv_name"
            android:digits="abcderf123"
            android:hint="請輸入用戶名。。"
            android:textColor="#ff0000"
            android:textColorHint="#0000ff"
            android:inputType="number"
            android:textCursorDrawable="@null" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp" >

        <TextView
            android:id="@+id/tv_pwd"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="用戶密碼:" />

        <EditText
            android:id="@+id/et_pwd"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_toRightOf="@id/tv_pwd"
            android:digits="abc123"
            android:hint="請輸入用戶密碼。。"
            android:inputType="textPassword"
            android:textColor="#ff0000"
            android:textColorHint="#0000ff"
            android:textCursorDrawable="@null" >
                <requestFocus/>
            </EditText>

        

    </RelativeLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="50dp"
        android:text="登錄" />

</LinearLayout>

Java代碼

package com.qianfeng.editext;

import android.app.Activity;
import android.os.Bundle;
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 implements OnClickListener{
    //聲明相應的控件
    private EditText et_name,et_pwd;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //通過相應的id  用findViewById找出控件
        et_name =(EditText) findViewById(R.id.et_name);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        btn  = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }


    @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;
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.button1:
            String  name = et_name.getText().toString().trim();//通過getText() 得到相應的文本內容
            String  pwd = et_pwd.getText().toString().trim();//通過getText() 得到相應的文本內容
            
            if("abc123".equals(name)&&"abc".equals(pwd)){
                Toast.makeText(getApplicationContext(), "登錄成功", Toast.LENGTH_SHORT).show();
            }else if("".equals(name)){
                Toast.makeText(getApplicationContext(), "請輸入用戶名稱", Toast.LENGTH_SHORT).show();
            }else if("".equals(pwd)){
                Toast.makeText(getApplicationContext(), "請輸入用戶密碼", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(getApplicationContext(), "用戶名稱和密碼不一致", Toast.LENGTH_SHORT).show();
            }
            break;

        default:
            break;
        }
    }
    
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章