话痨博主的碎碎念:嗯…感觉上一篇要写完的话写的太长了….所以就拆开了_(:з」∠)_
所以这一篇呢是实现登录界面(虽然我们老师说讲登录注册功能是最low的……)
不管了!反正我啥也不会!
欢迎指正!欢迎指教!欢迎批评!
这里把我参考的网址都记录下来:
慕课网:http://www.imooc.com/video/1444/0
小KING教你做android项目(二)—实现登陆页面并跳转和简单的注册页面
这里再展示一下原型 <吃藕这件事情…咱们藏在心里不要说出来就好>
我们要实现的页面大致是这样的:<背景是轮转播放图片>
欢迎页:
登录页:
预备知识
参考学姐博客:初识安卓Android(1)
凑不要脸的贴上自己的上一篇:【大白鹿学习andriod开发 第二弹】基本组件学习+实现欢迎登录界面(1)
好噜,我们开始吧
首先我们需要建立我们的第二个界面–登陆界面
分别新建如下两个文件(loginActivity.java和activity_login.xml):
并在主的andriodManifest.xml文件下新增:
<activity
android:name=".loginActivity"></activity>
来注册我们的新界面
同样,我为了实现页面跳转,新建了一个主页面和一个注册页面。
根据前两节的知识,我们可以简单的对我们的界面进行布局,如下:
代码:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="22dp"
android:layout_y="93dp"
android:text="您好,请选择"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textSize="24sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="22dp"
android:layout_y="148dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/pasw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username"
android:textColor="#ffffff"
android:textSize="20sp" />
<EditText
android:id="@+id/usernameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入账号"
android:inputType="textPersonName"
android:textColor="#ffffff"
android:textColorHint="#CCCCCC" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textColor="#ffffff"
android:textSize="20sp" />
<EditText
android:id="@+id/paswText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入密码"
android:inputType="textPassword"
android:textColor="#ffffff"
android:textColorHint="#CCCCCC" />
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="284dp"
android:layout_height="38dp"
android:background="#41C7D6"
android:text="登录"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="271dp"
android:layout_height="wrap_content" >
<Button
android:id="@+id/forgetPasw"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#41C7D6"
android:background="@android:color/transparent"<!-- 除去按钮默认效果 -->
android:text="忘记密码?" />
<Button
android:id="@+id/register"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#41C7D6"
android:background="@android:color/transparent"
android:text="新用户注册" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="92dp"
android:layout_y="376dp" >
</LinearLayout>
</AbsoluteLayout>
这里需要补充的知识是关于button控件的。
大家可以去慕课网看看讲解button这一节的内容:
5.1 Android-按钮概述-慕课网
好啦,页面也有了,我们要开始做一些简单的效果了。
比如说,实现点击登录按钮,当用户名密码正确时,跳转到首页。反之,出现“用户名或密码不正确的提示”。
这个过程需要一个OnClickListener来监听我们的登录按钮。在获得点击事件之后,进行简单的逻辑判断(还没有用到数据库哦)来达成我们想要实现的效果。
相应的按钮监听的课程在这里:http://www.imooc.com/video/1545
下面我简要的写一下我的学习笔记:
事件监听有很多种,这里主要以onclickListener为主。要注意的是,所有的控件都有相应的事件监听。
我们可以通过.setOnClickListener(OnClickListener)来对其进行相应的活动编辑。
实现事件监听有三种方式:
- 匿名内部类
- 外部类
- 接口
其中,我们通常是用匿名内部类的方式来实现事件监听。而外部类可以在所有按键使用时,执行同一个动作的情况下来使用,以达到减少代码量的目的。
这里我们使用匿名内部类来实现功能:
大致有三步:
- 初始化当前所需的控件
- 设置控件监听
- 实现点击后要操作的动作
首先是初始化一个登录按钮,还有两个输入框:
Button loginBtn=(Button)findViewById(R.id.login);
final EditText name=(EditText)findViewById(R.id.usernameText);
final EditText psw=(EditText)findViewById(R.id.paswText);
然后对我们的loginBtn进行监听:
loginBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// 这里实现监听得到后要执行的动作
});
最后,添加点击后要操作的事情:
String username=name.getText().toString().trim();//将获取的用户输入的editText转换为字符串,以便简单的逻辑判断
String password=psw.getText().toString().trim();
if(!username.equals("admin")||!password.equals("1111"))
{//设定页面跳转条件 这里需要用户名为admin以及密码为1111时跳转
Toast.makeText(loginActivity.this, "用户名或密码错误:"+username+"."+password, Toast.LENGTH_SHORT).show();//否则出现错误提示
}
else{
Intent intent=new Intent();
intent.setClass(loginActivity.this, MainActivity.class);
startActivity(intent);
}
然后想到要实现多个按钮的监听,就采用了外部类的方法,根据点击传入的view来判断是哪个按钮被点击了,代码如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//将布局xml文件引入当前activity之中
setContentView(R.layout.activity_login);
Button loginBtn=(Button)findViewById(R.id.login);
Button testBtn=(Button)findViewById(R.id.register);
loginBtn.setOnClickListener(new myListener());
testBtn.setOnClickListener(new myListener());
}
public class myListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.login){
EditText name=(EditText)findViewById(R.id.usernameText);
EditText psw=(EditText)findViewById(R.id.paswText);
String username=name.getText().toString().trim();
String password=psw.getText().toString().trim();
if(username.equals("")||password.equals("")){
Toast.makeText(loginActivity.this, "您还没有输入完整哦~", Toast.LENGTH_SHORT).show();
}
else if(!username.equals("admin")||!password.equals("1111")){
Toast.makeText(loginActivity.this, "用户名或密码错误:"+username+"."+password, Toast.LENGTH_SHORT).show();
}
else{
Intent intent=new Intent();
intent.setClass(loginActivity.this, MainActivity.class);
startActivity(intent);
}
}
if(v.getId()==R.id.register){
Intent intent=new Intent();
intent.setClass(loginActivity.this, RegisterActivity.class);
startActivity(intent);
}
}
}
好啦,到这里简单的登录功能就实现啦。