AndroidStudio入门篇-创建登陆页面

  初学android小白一只,本来实验室是用Eclipse来做android开发,但是在配置环境的过程中出现了一些bug,一直找不到R.java,经历了重装ADT、重新配置SDK无果之后,弃坑选择了AndroidStudio(之前小小的使用过一下),等解决了eclipse之后再做后续更新,接下来就是关于AndroidStudio入门的一些介绍;

   新建一个Project(选择empty Activity)

 1、 目录解析

   每一个activity对应一个页面,JAVA里面存放的就是我们所有的Java代码,一个Java文件对应一个Activity,

   每创建一个Activity就要去AndroidManifest.xml里面注册(一定要!!!不然跳转会出错),

   res文件下存放我们在项目中使用到的图片、字符串、布局等资源,在本次示例中只涉及到了layout布局;我们能够使用到的主要就是这3个地方

AndroidManifest.xml注册login和index

2、 登录界面

(1)页面设计-activity_index

 

可以在design菜单栏中选择一些常用的按钮控件什么的,自己拖拉生成一个简易的界面,比较简单方便,也容易理解,对小白很友好,生成的XML文件如下:

<TextView
        android:text="用户名"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="49dp"
        android:id="@+id/user" />

    <EditText
        android:hint="请输入用户名"//提示用户输入用户名
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="21dp"
        android:layout_marginEnd="21dp"
        android:layout_marginTop="36dp"
        android:id="@+id/username" />

    <TextView
        android:text="密码"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/username"
        android:layout_alignLeft="@+id/user"
        android:layout_alignStart="@+id/user"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="75dp"
        android:id="@+id/pwd" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:layout_alignBottom="@+id/pwd"
        android:layout_alignRight="@+id/username"
        android:layout_alignEnd="@+id/username"
        android:id="@+id/password" />

    <Button
        android:text="登录"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password"
        android:layout_alignRight="@+id/password"
        android:layout_alignEnd="@+id/password"
        android:layout_marginRight="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginTop="79dp"
        android:id="@+id/login" />
</RelativeLayout>

(2)login.java
login.java主要就是实现login界面的功能啦
public class login extends AppCompatActivity {
    private Button login;
    EditText username;
    EditText password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        login=(Button)findViewById(R.id.login);
        username=(EditText)super.findViewById(R.id.username);//获取用户输入的用户名
        password=(EditText)super.findViewById(R.id.password);//获取用户密码
        login.setOnClickListener(new View.OnClickListener()//侦听登录点击事件
        {
            public void onClick(View v)
            {//验证用户名密码是否符合要求
                if(username.getText().toString().equals("admin")&&password.getText().toString().equals("888888"))
                {
                    Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();//提示用户登陆成功
                    Intent t=new Intent(login.this,index.class);//从login页面跳转到index界面
                     startActivity(t);
                }
                if(!username.getText().toString().equals("admin")||!password.getText().toString().equals("888888"))
                {
                    Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();//提示用户用户名或密码错误
                }
            }
        }
        );
    }
}

3、界面效果

 

 点击登录就会显示登录成功、跳转到首页(首页表示只是写了个欢迎登陆就不拿出来丢人啦)

 还有就是一直想在手机上测试一下效果,但是一直显示找不到device,so.....继续解决,解决了来更新

猜你喜欢

转载自littleschnappi.iteye.com/blog/2381967