Android 控制項之TextView、EditView、CheckBox

來源:互聯網
上載者:User

Android 控制項之TextView、EditView、CheckBox

--學習筆記3(金海建)

目的:通過一個登陸框,來學習TextView、EditView、CheckBox的使用

建立一個工程,然後設計如下ui

Main.xml layout代碼

 

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"><br /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/username" android:id="@+id/password"></TextView><br /><EditText android:layout_height="wrap_content" android:id="@+id/EditPassword" android:layout_width="match_parent"></EditText><br /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/username" android:text="@string/password"></TextView><br /><EditText android:layout_height="wrap_content" android:id="@+id/EditPassword" android:layout_width="fill_parent"></EditText><br /><CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/CheckRemenber" android:text="@string/remenber"></CheckBox><br /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Login" android:text="@string/lonin"></Button><br /></LinearLayout><br /> 

 

 

TextView

設定username顏色為白色、字型大小為20、設定粗體

設定 password顏色為白色、字型為18

 

m_username = (TextView)this.findViewById(R.id.username);<br /> m_password = (TextView)this.findViewById(R.id.password);<br /> m_username.setTextSize(18);<br /> m_password.setTextSize(20);</p><p> // 設定顏色,通過Color的rgb設定<br /> //m_username.setTextColor(Color.rgb(0xFF, 0xFF, 0xFF));<br /> // 通過Color內建顏色<br /> //m_username.setTextColor(Color.WHITE);<br /> // 通過getResources的getColor方法來擷取<br /> m_username.setTextColor(this.getResources().getColor(R.color.usernamecolor));<br /> Typeface tyface = Typeface.create("", Typeface.BOLD);<br /> m_username.setTypeface(tyface);</p><p> m_password.setTextColor(this.getResources().getColor(R.color.usernamecolor));<br /> m_password.setTypeface(tyface);<br /> 

EditView

設定username 的提示為“請輸入使用者名稱”,設定只能輸入數字

設定 password 的提示為”用輸入密碼”,設定屬性為密碼屬性,設定只能輸入數字

提示調用

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

m_editpassword.setHint("請輸入密碼");

設定只能輸入數字,有三種方法

方法1:直接產生DigitsKeyListener對象就可以了

m_editusername.setKeyListener(new DigitsKeyListener(false, true));

方法2:在EditText中設定屬性

android:inputType="number"

 

方法3:建立一個char[],在裡面添加允許輸入的字元

m_editusername.setKeyListener(new NumberKeyListener()<br />{<br />char acceptnumber[] = {'1','2','3','4','5','6','7','8','9','0'};<br />protected char[] getAcceptedChars() {<br />return acceptnumber;<br />}<br />public int getInputType() {<br />return 0;<br />}<br />}); 

Button

處理onClick事件

m_btLogin.setOnClickListener(new Button.OnClickListener(){<br />public void onClick(View v) {<br />Toast toast = Toast.makeText(Login.this, "the user name is " + m_editusername.getText() + " the psd is " + m_editpassword.getText(), Toast.LENGTH_LONG);<br />toast.show();<br />}<br />}); 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.