Android 控件之TextView、EditView、CheckBox
--学习笔记3(金海建)
目的:通过一个登陆框,来学习TextView、EditView、CheckBox的使用
新建一个工程,然后设计如下ui
Main.xml layout代码
<?xml version="1.0" encoding="utf-8"?> <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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/username" android:id="@+id/password"></TextView> <EditText android:layout_height="wrap_content" android:id="@+id/EditPassword" android:layout_width="match_parent"></EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/username" android:text="@string/password"></TextView> <EditText android:layout_height="wrap_content" android:id="@+id/EditPassword" android:layout_width="fill_parent"></EditText> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/CheckRemenber" android:text="@string/remenber"></CheckBox> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Login" android:text="@string/lonin"></Button> </LinearLayout>
TextView
设置username颜色为白色、字体大小为20、设置粗体
设置 password颜色为白色、字体为18
m_username = (TextView)this.findViewById(R.id.username); m_password = (TextView)this.findViewById(R.id.password); m_username.setTextSize(18); m_password.setTextSize(20); // 设置颜色,通过Color的rgb设置 //m_username.setTextColor(Color.rgb(0xFF, 0xFF, 0xFF)); // 通过Color内置颜色 //m_username.setTextColor(Color.WHITE); // 通过getResources的getColor方法来获取 m_username.setTextColor(this.getResources().getColor(R.color.usernamecolor)); Typeface tyface = Typeface.create("", Typeface.BOLD); m_username.setTypeface(tyface); m_password.setTextColor(this.getResources().getColor(R.color.usernamecolor)); m_password.setTypeface(tyface);
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() { char acceptnumber[] = {'1','2','3','4','5','6','7','8','9','0'}; protected char[] getAcceptedChars() { return acceptnumber; } public int getInputType() { return 0; } });
Button
处理onClick事件
m_btLogin.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { Toast toast = Toast.makeText(Login.this, "the user name is " + m_editusername.getText() + " the psd is " + m_editpassword.getText(), Toast.LENGTH_LONG); toast.show(); } });