Android实现登录

 登录界面布局文件
   
   

1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#E6E6E6" 6 android:orientation="vertical"> 7 <ImageView 8 android:id="@+id/iv_head" 9 android:layout_width="100dp" 10 android:layout_height="100dp" 11 android:layout_centerHorizontal="true" 12 android:layout_marginTop="40dp" 13 android:src="@drawable/ic_launcher11"/> 14 <LinearLayout 15 android:id="@+id/layout" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:layout_below="@+id/iv_head" 19 android:layout_margin="10dp" 20 android:background="#FFFFFF" 21 android:orientation="vertical"> 22 <RelativeLayout 23 android:id="@+id/rl_username" 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" 26 android:layout_margin="10dp"> 27 <TextView 28 android:id="@+id/tv_name" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:layout_centerVertical="true" 32 android:text="账号"/> 33 <EditText 34 android:id="@+id/et_number" 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content" 37 android:layout_marginLeft="5dp" 38 android:layout_toRightOf="@+id/tv_name" 39 android:background="@null"/> 40 </RelativeLayout> 41 <View 42 android:layout_width="match_parent" 43 android:layout_height="2dp" 44 android:background="#E6E6E6"/> 45 <RelativeLayout 46 android:id="@+id/rl_userpsd" 47 android:layout_width="match_parent" 48 android:layout_height="wrap_content" 49 android:layout_margin="10dp"> 50 <TextView 51 android:id="@+id/tv_psw" 52 android:layout_width="wrap_content" 53 android:layout_height="wrap_content" 54 android:layout_centerVertical="true" 55 android:text="密码"/> 56 <EditText 57 android:id="@+id/et_password" 58 android:layout_width="match_parent" 59 android:layout_height="wrap_content" 60 android:layout_marginLeft="5dp" 61 android:layout_toRightOf="@+id/tv_psw" 62 android:inputType="textPassword" 63 android:background="@null"/> 64 </RelativeLayout> 65 66 </LinearLayout> 67 <Button 68 android:id="@+id/btn_login" 69 android:onClick="student" 70 android:layout_width="match_parent" 71 android:layout_height="30dip" 72 android:layout_below="@+id/layout" 73 android:layout_centerHorizontal="true" 74 android:layout_marginLeft="10dp" 75 android:layout_marginRight="10dp" 76 android:layout_marginTop="20dp" 77 android:background="#3C8DC4" 78 android:text="登录" 79 android:textColor="#FFFFFF"/> 80 <Button 81 android:id="@+id/signUp" 82 android:layout_width="wrap_content" 83 android:layout_height="wrap_content" 84 android:text="注册" 85 android:layout_marginRight="10dp" 86 android:background="#E6E6E6" 87 android:textColor="#000000" 88 android:layout_marginTop="21dp" 89 android:layout_centerHorizontal="true" 90 android:layout_below="@+id/btn_login" 91 android:layout_alignParentRight="true"/> 92 93 94 </RelativeLayout>
  1 package com.itcast.test03;
  2 import java.io.BufferedReader;
  3 import java.io.InputStream;
  4 import java.io.InputStreamReader;
  5 import java.util.ArrayList;
  6 import java.util.List;
  7 
  8 import org.apache.http.HttpEntity;
  9 import org.apache.http.HttpResponse;
 10 import org.apache.http.NameValuePair;
 11 import org.apache.http.client.HttpClient;
 12 import org.apache.http.client.entity.UrlEncodedFormEntity;
 13 import org.apache.http.client.methods.HttpPost;
 14 import org.apache.http.impl.client.DefaultHttpClient;
 15 import org.apache.http.message.BasicNameValuePair;
 16 import org.json.JSONArray;
 17 import org.json.JSONObject;
 18 
 19 import android.os.Bundle;
 20 import android.app.Activity;
 21 import android.content.Intent;
 22 import android.text.TextUtils;
 23 import android.util.Log;
 24 import android.view.Menu;
 25 import android.view.View;
 26 import android.widget.EditText;
 27 import android.widget.Toast;
 28 
 29 public class MainActivity extends Activity {
 30     private EditText et_username;
 31     private EditText et_userPsd;
 32     
 33     @Override
 34     protected void onCreate(Bundle savedInstanceState) {
 35         super.onCreate(savedInstanceState);
 36         setContentView(R.layout.activity_main);
 37         et_username = (EditText)findViewById(R.id.et_number);
 38         et_userPsd = (EditText)findViewById(R.id.et_password);
 39     }
 40  public void student(View view){
 41           String name = et_username.getText().toString();//把EditTextable类型转化为字符串并赋值给name、password
 42         String password = et_userPsd.getText().toString();
 43        if(TextUtils.isEmpty(name)||TextUtils.isEmpty(password)){//判断输入的用户和密码是否有为空的
 44            Toast.makeText(this, "用户名和密码不能为空", 0).show();
 45            Intent intent01 = new Intent(this,StudentMainActivity.class);
 46            startActivity(intent01);
 47        }else{
 48         try{     
 49                     
 50                     HttpClient client = new DefaultHttpClient();//获取HettpClient对象
 51                     //指定访问地址
 52                     String path = "http://10.6.78.213:2016/xampp/sse/index.php/home/Index/server_info";
 53                     HttpPost httpPost = new HttpPost(path);//Post方式请求网络
 54                     //请求服务器并获取服务器返回的信息
 55                     HttpResponse response = client.execute(httpPost);
 56                     //获取状态码
 57                     int code = response.getStatusLine().getStatusCode();
 58                     if(code == 200){
 59                         //将输入流转换成字符串
 60                         InputStream is = response.getEntity().getContent();
 61                         //将字节输入转换成字符输入流
 62                         InputStreamReader in = new InputStreamReader(is);
 63                         //对字符流对象进行包装
 64                         BufferedReader bufferedReader = new BufferedReader(in);
 65                         //StringBuilder(String str)   构造一个字符串生成器,并初始化为指定的字符串内容。
 66                         StringBuilder builder = new StringBuilder();
 67                         for (String s = bufferedReader.readLine(); s != null; s = bufferedReader
 68                                 .readLine()) {
 69                             builder.append(s);
 70                         }
 71                         Log.i("cat", ">>>>>>" + builder.toString());
 72                         String UName;
 73                         String UPass;
 74                         /*JsonObject 就是常说的 json。是一种重要的数据传输对象。
 75                          *  其格式为{"key1":value1,"key2",value2....};key 必须是字符串。
 76                             很像map对不对,一个key,一个value。
 77                             因为ajax请求不刷新页面,但配合js可以实现局部刷新,因此json常常被用来作为异步请求的返回对象使用。*/
 78                         JSONObject jsonObject = new JSONObject(builder.toString());
 79                         UName = jsonObject.getString("user_name");
 80                         UPass = jsonObject.getString("user_password");
 81                        
 82                         if(UName.equals(name)&&UPass.equals(password))
 83                         {
 84                             Toast.makeText(this, "用户名密码输入正确", 0).show();
 85                             Intent intent = new Intent(this,StudentMainActivity.class);
 86                             startActivity(intent);
 87                             }
 88 
 89                         
 90                         else
 91                         {
 92                             Toast.makeText(this, "用户名密码输入不正确", 0).show();
 93                         }
 94                     }
 95                    
 96                 }catch(Exception e){
 97                     e.printStackTrace();
 98                    
 99                 }                             
100     }      
101  }
102  
103  }
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="fill_parent"
 3     android:layout_height="fill_parent"
 4     android:orientation="vertical" 
 5      android:background="#E6E6E6">
 6 
 7     
 8         <LinearLayout
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:orientation="vertical">
12 
13             <RelativeLayout
14                 android:layout_width="match_parent"
15                 android:layout_height="match_parent"
16                   android:background="#000000">
17 
18                 <TextView
19                     android:id="@+id/textView1"
20                     android:layout_width="wrap_content"
21                     android:layout_height="wrap_content"
22                     android:layout_centerHorizontal="true"
23                     android:layout_centerVertical="true"
24                     android:textSize="25sp"
25                     android:textColor="#FFFFFF"
26                     android:layout_marginLeft="10dp"
27                      android:layout_marginRight="10dp"
28                     android:text="软件学院的通知和公告" />
29 
30             </RelativeLayout>
31         </LinearLayout>
32 
33          <GridView
34             android:id="@+id/gridView"
35             android:background="#FFFFFF"
36             android:layout_width="match_parent"
37             android:layout_height="match_parent"
38             android:numColumns="3"
39             android:verticalSpacing="10dp"
40             android:horizontalSpacing="10dp"
41             android:columnWidth="90dp"
42             android:stretchMode="columnWidth"
43             android:gravity="center" >
44         </GridView>
45 
46    
47 
48 </LinearLayout>

1

  1 package com.itcast.test03;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 
  6 import android.os.Bundle;
  7 import android.app.Activity;
  8 import android.view.Menu;
  9 import android.view.View;
 10 import android.view.ViewGroup;
 11 import android.widget.AdapterView;
 12 import android.widget.AdapterView.OnItemClickListener;
 13 import android.widget.BaseAdapter;
 14 import android.widget.GridView;
 15 import android.widget.ImageView;
 16 import android.widget.SimpleAdapter;
 17 import android.widget.TextView;
 18 
 19 public class StudentMainActivity extends Activity {
 20     private GridView gridview;
 21     private GridView gridview1;
 22     @Override
 23     protected void onCreate(Bundle savedInstanceState) {
 24         super.onCreate(savedInstanceState);
 25         setContentView(R.layout.activity_student_main);
 26         gridview = (GridView)findViewById(R.id.gridView);
 27         ArrayList<HashMap<String,Object>> lstImageItem = new ArrayList<HashMap<String,Object>>();
 28         for(int i = 0;i<20;i++){
 29             HashMap<String,Object> map = new HashMap<String, Object>();
 30             if(i==1){    
 31                 map.put("ItemImage", R.drawable.ic_launcher02);
 32                 map.put("ItemText", "实践教学管理系统");
 33                 lstImageItem.add(map);
 34             }else if(i==2){    
 35                 map.put("ItemImage", R.drawable.ic_launcher06);
 36                 map.put("ItemText", "毕业设计管理系统");
 37                 lstImageItem.add(map);
 38             }else if(i==3){    
 39                 map.put("ItemImage", R.drawable.ic_launcher02);
 40                 map.put("ItemText", "开放实验管理系统");
 41                 lstImageItem.add(map);
 42             }else if(i==4){    
 43                 map.put("ItemImage", R.drawable.ic_launcher03);
 44                 map.put("ItemText", "实习实训管理系统");
 45                 lstImageItem.add(map);
 46             }else if(i==5){    
 47                 map.put("ItemImage", R.drawable.ic_launcher05);
 48                 map.put("ItemText", "班级事务管理系统");
 49                 lstImageItem.add(map);
 50             }else if(i==6){    
 51                 map.put("ItemImage", R.drawable.ic_launcher06);
 52                 map.put("ItemText", "综合实践管理系统");
 53                 lstImageItem.add(map);
 54             }else if(i==7){    
 55                 map.put("ItemImage", R.drawable.ic_launcher04);
 56                 map.put("ItemText", "其他事物2管理系统");
 57                 lstImageItem.add(map);
 58             }else if(i==8){    
 59                 map.put("ItemImage", R.drawable.ic_launcher08);
 60                 map.put("ItemText", "其他事物3管理系统");
 61                 lstImageItem.add(map);
 62             }else if(i==9){    
 63                 map.put("ItemImage", R.drawable.ic_launcher01);
 64                 map.put("ItemText", "其他事物4管理系统");
 65                 lstImageItem.add(map);
 66             }else if(i==10){    
 67                 map.put("ItemImage", R.drawable.ic_launcher06);
 68                 map.put("ItemText", "毕业设计管理系统");
 69                 lstImageItem.add(map);
 70             }else if(i==11){    
 71                 map.put("ItemImage", R.drawable.ic_launcher02);
 72                 map.put("ItemText", "开放实验管理系统");
 73                 lstImageItem.add(map);
 74             }else if(i==12){    
 75                 map.put("ItemImage", R.drawable.ic_launcher03);
 76                 map.put("ItemText", "实习实训管理系统");
 77                 lstImageItem.add(map);
 78             }else if(i==13){    
 79                 map.put("ItemImage", R.drawable.ic_launcher05);
 80                 map.put("ItemText", "班级事务管理系统");
 81                 lstImageItem.add(map);
 82             }else if(i==14){    
 83                 map.put("ItemImage", R.drawable.ic_launcher06);
 84                 map.put("ItemText", "综合实践管理系统");
 85                 lstImageItem.add(map);
 86             }else if(i==15){    
 87                 map.put("ItemImage", R.drawable.ic_launcher04);
 88                 map.put("ItemText", "其他事物2管理系统");
 89                 lstImageItem.add(map);
 90             }else if(i==15){    
 91                 map.put("ItemImage", R.drawable.ic_launcher08);
 92                 map.put("ItemText", "其他事物3管理系统");
 93                 lstImageItem.add(map);
 94             }else if(i==17){    
 95                 map.put("ItemImage", R.drawable.ic_launcher01);
 96                 map.put("ItemText", "其他事物4管理系统");
 97                 lstImageItem.add(map);
 98             }
 99         }
100         SimpleAdapter saImageItems = new SimpleAdapter(this,lstImageItem,R.layout.next_activity_student_main,new String[]{"ItemImage","ItemText"},new int[] {R.id.ItemImage,R.id.ItemText});
101         gridview.setAdapter(saImageItems);
102         gridview.setOnItemClickListener(new ItemClickListener());
103         
104     } 
105    
106     class ItemClickListener implements OnItemClickListener{
107 
108         @Override
109         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
110                 long arg3) {
111             // TODO Auto-generated method stub
112             HashMap<String, Object> item = (HashMap<String,Object>)arg0.getItemAtPosition(arg2);
113             setTitle((String)item.get("ItemText"));
114             
115         }
116         
117     }
118     @Override
119     public boolean onCreateOptionsMenu(Menu menu) {
120         // Inflate the menu; this adds items to the action bar if it is present.
121         getMenuInflater().inflate(R.menu.main, menu);
122         return true;
123     }
124     
125     
126 }
添加访问权限   
<uses-permission android:name="android.permission.INTERNET"/>

 



 

posted on 2016-04-10 21:17  kang_ya_ping  阅读(857)  评论(0编辑  收藏  举报

导航