布局文件
<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;
}
}
}