android登录小界面

来源:互联网 发布:java 日志 实现 编辑:程序博客网 时间:2024/04/30 11:53

本文介绍的是android 一个简单的登录界面,这个界面包括对EditText,.Button,CheckBox等控件的应用,还包括了Button的监听事件,EditText的文本改变事件。下面附上主要的源代码,并附上界面运行效果图,若发现问题欢迎提出加以交流。

主要的布局代码如下:

界面一:

<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:background="@drawable/bj"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="账号:"
            android:textColor="#000"
            android:textSize="17sp" />
    <EditText
        android:id="@+id/ed1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
          android:hint="请输入账号"
        android:ems="10"
        android:textSize="15pt" />
     </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="密码:"
            android:textColor="#000"
            android:textSize="17sp" />
        <EditText
            android:id="@+id/ed2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="请输入密码"
            android:textSize="15pt" />
     </LinearLayout>
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:orientation="vertical">
          <TextView
              android:id="@+id/tv3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="8dp"
              android:text="请选择你的爱好:"
              android:textColor="#000"
              android:textSize="17sp" />
       <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">
  <CheckBox
      android:id="@+id/c1"
       android:layout_height="wrap_content" 
      android:layout_width="wrap_content"
       android:text="打乒乓球" 
           android:layout_marginBottom ="5dp"
       android:button="@drawable/check_style" 
       />
   <CheckBox
      android:id="@+id/c2"
       android:layout_height="wrap_content" 
      android:layout_width="wrap_content"
       android:text="打台球" 
       android:layout_marginLeft="10dp"
       android:layout_marginBottom ="5dp"
       android:button="@drawable/check_style" 
       />
   <CheckBox
       android:id="@+id/c3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:button="@drawable/check_style"
         android:layout_marginLeft="10dp"
             android:layout_marginBottom ="5dp"
       android:text="看书" />
       </LinearLayout>
    </LinearLayout>
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">


     <Button
         android:id="@+id/bt1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginBottom="5dp"
          android:layout_marginLeft="30dp"
          android:onClick="click"
         android:text="登录" />


     <Button
         android:id="@+id/bt2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginBottom="5dp"
         android:layout_marginLeft="100dp"
         android:text="重置" />


        </LinearLayout>


</LinearLayout>

界面二:<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:background="@drawable/b"
    android:orientation="vertical" >
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/tv3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="账号:"
            android:textColor="#000"
            android:textSize="17sp" />
    <EditText
        android:id="@+id/ed4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="15pt" />
     </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/tv5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="密码:"
            android:textColor="#000"
            android:textSize="17sp" />
        <EditText
            android:id="@+id/ed5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:textSize="15pt" />
         </LinearLayout>
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/tv6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="爱好:"
            android:textColor="#000"
            android:textSize="17sp" />
    <EditText
        android:id="@+id/ed6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="10pt" />
     </LinearLayout>


</LinearLayout>

主要事件代码:

package com.example.dl1;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
  private EditText  ed1,ed2;
    private Button bt1,bt2;
    private CheckBox c1,c2,c3;
    private String s1,s2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=( EditText )findViewById(R.id.ed1);
ed2=( EditText )findViewById(R.id.ed2);
bt1=( Button)findViewById(R.id.bt1);
bt2=( Button)findViewById(R.id.bt2);
c1=( CheckBox )findViewById(R.id.c1);
c2=( CheckBox )findViewById(R.id.c2);
c3=( CheckBox )findViewById(R.id.c3);

bt1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) 
{
// TODO Auto-generated method stub
  String s3=" ";
   s1=ed1.getText().toString();
s2=ed2.getText().toString();
if(s1.equals("wll")&&s2.equals("abc"))
{
if(c1.isChecked())
 {
s3+=c1.getText().toString()+",";
  }
if(c2.isChecked())
{
s3+=c2.getText().toString()+",";
}
if(c3.isChecked())
{
s3+=c3.getText().toString()+",";
}
Bundle data=new Bundle();
        data.putString("S1", s1);
        data.putString("S2", s2);
        data.putString("S3", s3);
      Intent it =new Intent(MainActivity.this,MainA1ctivity.class);
      it.putExtras(data);
      startActivity(it);   
}
else
{
Toast.makeText(getApplicationContext(), "你的账号或密码有问题",Toast.LENGTH_SHORT).show();
 ed1.setText("");
 ed2.setText("");
}

}
 

});
bt2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
ed1.setText("");
ed2.setText("");
}
});
}


@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;
}


}

事件二:

package com.example.dl1;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.EditText;


public class MainA1ctivity extends Activity {
private EditText  ed4,ed5,ed6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_a1ctivity);
ed4=( EditText )findViewById(R.id.ed4);
ed5=( EditText )findViewById(R.id.ed5);
ed6=( EditText )findViewById(R.id.ed6);
Intent intent=getIntent();
Bundle bundle =intent.getExtras();
        String t1=bundle.getString("S1");
        String t2=bundle.getString("S2");
        String t3=bundle.getString("S3");
        ed4.setText(t1);
        ed5.setText(t2);
        ed6.setText(t3);
}


@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_a1ctivity, menu);
return true;
}


}


0 0
原创粉丝点击