第二章 吸引你的眼球—UI編程(2),第二章ui

來源:互聯網
上載者:User

第二章 吸引你的眼球—UI編程(2),第二章ui
2.1.3文本編輯—編輯框(EditText)

EditText在我們開發中也是經常要用到的組件。比如,要實現一個登入介面,需要使用者輸入帳號、密碼、郵件等資訊,這裡就需要使用EditText組件來獲得使用者輸入的內容,下面,我們就以一個登入介面為例,來看看EditText是怎麼使用的。

 

1)在布局檔案中定義一個TextView(用來響應按鈕事件),兩個EditText組件(一個用來記錄使用者名稱,一個用來記錄密碼)一個登入按鈕和一個取消按鈕,代碼如下:

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

<LinearLayout

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

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

    <EditText

        android:id="@+id/name_edittext"

        android:layout_width="200dp"

        android:layout_height="wrap_content"

        android:hint="請輸入使用者名稱"/>

    <EditText

        android:id="@+id/pwd_edittext"

        android:layout_width="200dp"

        android:layout_height="wrap_content"

        android:password="true"

        android:hint="請輸入密碼"/>

    <LinearLayout

        android:orientation="horizontal"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content">

        <Button

            android:id="@+id/ok_button"

            android:layout_width="80dp"

            android:layout_height="wrap_content"

            android:text="登入"/>

        <Button

            android:id="@+id/cancel_button"

            android:layout_width="80dp"

            android:layout_height="wrap_content"

            android:text="取消"/>

    </LinearLayout>

</LinearLayout>

 

EditText中android:hint這個屬性是當使用者沒有輸入任何內容的時候預設顯示的提示資訊。當然,我們也可以在代碼中輕鬆地實現,例如:

EditText editView = (EditText)findViewById(R.id.name_edittext);

editView.setHint("請輸入使用者名稱");

 

而android:password這個屬性是用於輸入密碼之類涉及隱私需要保密的資訊。

 

2)點擊“登入”按鈕時,判斷使用者名稱和密碼是否都已輸入,若是則在TextView中顯示“登入成功”,否則的話顯示“請檢查輸入資訊”;點擊“取消”按鈕時,將TextView和EditText中的文本全部清空,部分代碼如下:

okButton = (Button)findViewById(R.id.ok_button);

        cancelButton = (Button)findViewById(R.id.cancel_button);

        nameEditText = (EditText)findViewById(R.id.name_edittext);

        pwdEditText = (EditText)findViewById(R.id.pwd_edittext);

        statusTextView = (TextView)findViewById(R.id.status_textview);

        okButton.setOnClickListener(new View.OnClickListener() {

            

             @Override

             public void onClick(View v) {

                 if(nameEditText.length()==0||pwdEditText.length()==0){

                     statusTextView.setText("請檢查輸入資訊");

                 }else{

                     statusTextView.setText("登入成功");

                 }

             }

        });

        cancelButton.setOnClickListener(new View.OnClickListener() {

            

             @Override

             public void onClick(View v) {

                 nameEditText.setText("");

                 pwdEditText.setText("");

                 statusTextView.setText("");

             }

        });

 

我們來看看效果,2-6、2-7、2-8所示:

圖2-6 初始介面

 

圖2-7 輸入資訊不完整

 

圖2-8 登入成功

 

經驗分享:

很多時候,EditText組件會預設獲得焦點,並彈出軟鍵盤,但是我們並不想要這樣的效果。這個時候,可以在布局檔案中加入以下代碼來避免這樣的情況出現:

<LinearLayout

    android:layout_width="0dp"

    android:layout_height="0dp"

    android:focusableInTouchMode="true"

    android:focusable="true" />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.