新人刚学习Android两周,写一个随笔算是对两周学习成果的巩固,不足之处欢迎各位建议和完善。
这次写的是一个简单登录案例,大概功能如下:
注册的账户信息用SharedPreferences存储;
登录成功后跳转到成功页面,在成功页面联网请求图片并写入到外部存储;
然后读出显示在成功页面;
注册xml代码:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.qf.login" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="14" 9 android:targetSdkVersion="21" /> 10 <!--联网权限 --> 11 <uses-permission android:name="android.permission.INTERNET"/> 12 <!--在SD卡中创建与删除文件权限 --> 13 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 14 <!--向SD卡写入数据权限 --> 15 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 16 17 <application 18 android:allowBackup="true" 19 android:icon="@drawable/ic_launcher" 20 android:label="@string/app_name" 21 android:theme="@style/AppTheme" > 22 <activity 23 android:name=".MainActivity" 24 android:label="@string/app_name" > 25 <intent-filter> 26 <action android:name="android.intent.action.MAIN" /> 27 28 <category android:name="android.intent.category.LAUNCHER" /> 29 </intent-filter> 30 </activity> 31 <activity 32 android:name=".Register" 33 ></activity> 34 <activity 35 android:name=".SuccessLogin" 36 ></activity> 37 </application> 38 39 </manifest>
MainActivity:
1 <LinearLayout 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:orientation="vertical" > 6 7 <LinearLayout 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:orientation="horizontal" 11 android:paddingTop="50dp" > 12 13 <TextView 14 android:layout_width="0dp" 15 android:layout_height="wrap_content" 16 android:layout_weight="1" 17 android:gravity="center_horizontal" 18 android:text="账号" /> 19 20 <EditText 21 android:id="@+id/et1" 22 android:layout_width="0dp" 23 android:layout_height="wrap_content" 24 android:layout_marginRight="5dp" 25 android:layout_weight="4" 26 android:background="@drawable/app_pref_bg" /> 27 </LinearLayout> 28 29 <LinearLayout 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:layout_marginTop="10dp" 33 android:orientation="horizontal" > 34 35 <TextView 36 android:layout_width="0dp" 37 android:layout_height="wrap_content" 38 android:layout_weight="1" 39 android:gravity="center_horizontal" 40 android:text="密码" /> 41 42 <EditText 43 android:id="@+id/et2" 44 android:layout_width="0dp" 45 android:layout_height="wrap_content" 46 android:layout_marginRight="5dp" 47 android:layout_weight="4" 48 android:background="@drawable/app_pref_bg" 49 android:inputType="textPassword" /> 50 </LinearLayout> 51 <LinearLayout 52 android:layout_width="match_parent" 53 android:layout_height="wrap_content" 54 android:layout_marginTop="10dp" 55 android:orientation="horizontal"> 56 <Button 57 android:id="@+id/btn1" 58 android:layout_width="70dp" 59 android:layout_height="45dp" 60 android:layout_marginLeft="90dp" 61 android:text="登录" 62 android:gravity="center" 63 /> 64 <Button 65 android:id="@+id/btn2" 66 android:layout_width="70dp" 67 android:layout_height="45dp" 68 android:layout_marginLeft="20dp" 69 android:text="注册" 70 android:gravity="center" 71 /> 72 73 </LinearLayout> 74 75 </LinearLayout>
注册布局xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="50dp" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="账号" /> <EditText android:id="@+id/et1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:layout_weight="4" android:background="@drawable/app_pref_bg" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="密码" /> <EditText android:id="@+id/et2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:layout_weight="4" android:background="@drawable/app_pref_bg" android:inputType="textPassword" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal" > <Button android:id="@+id/btn1" android:layout_width="70dp" android:layout_height="45dp" android:layout_marginLeft="110dp" android:gravity="center" android:text="注册" /> </LinearLayout> </LinearLayout>
登录成功布局xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#CEDDED" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="100dp" android:background="#1285F0" android:textSize="20sp" android:gravity="center" /> <Button android:id="@+id/btn" android:layout_width="150dp" android:layout_height="50dp" android:text="点击获取图片" android:background="@drawable/btn1" android:textSize="20sp" android:gravity="center" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" /> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" /> </LinearLayout>
登录主页面JAVA代码:
1 package com.qf.login; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.SharedPreferences; 7 import android.os.Bundle; 8 import android.text.TextUtils; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.EditText; 13 import android.widget.Toast; 14 15 public class MainActivity extends Activity implements OnClickListener { 16 private EditText et1; 17 private EditText et2; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 et1 = (EditText) findViewById(R.id.et1); 25 et2 = (EditText) findViewById(R.id.et2); 26 Button btn1 = (Button) findViewById(R.id.btn1); 27 Button btn2 = (Button) findViewById(R.id.btn2); 28 29 btn1.setOnClickListener(this); 30 btn2.setOnClickListener(this); 31 } 32 33 public void login() { 34 //获得输入的账户信息 35 String username = et1.getText().toString().trim(); 36 String password = et2.getText().toString().trim(); 37 //获得SharPreferences中存储的账户信息 38 SharedPreferences sp=getSharedPreferences("userinfo", Context.MODE_PRIVATE); 39 40 // d.判断用户名密码 41 if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) { 42 Toast.makeText(MainActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT) 43 .show(); 44 return; 45 }else if(username.equals(sp.getString("user", ""))&&password.equals(sp.getString("password", ""))){ 46 //跳转到登录成功页面 47 Intent intent=new Intent(MainActivity.this,SuccessLogin.class); 48 49 startActivity(intent); 50 }else if(!username.equals(sp.getString("user", ""))){ 51 Toast.makeText(MainActivity.this, "用户名不存在!请注册", Toast.LENGTH_SHORT).show(); 52 } 53 else{ 54 Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show(); 55 } 56 } 57 58 @Override 59 public void onClick(View v) { 60 switch (v.getId()) { 61 case R.id.btn1: 62 login(); 63 break; 64 case R.id.btn2: 65 Intent intent = new Intent(MainActivity.this, Register.class); 66 startActivity(intent); 67 break; 68 default: 69 break; 70 } 71 72 } 73 74 75 76 }
注册页面JAVA代码:
1 package com.qf.login; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.SharedPreferences; 7 import android.content.SharedPreferences.Editor; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.EditText; 13 import android.widget.Toast; 14 15 public class Register extends Activity{ 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 // TODO Auto-generated method stub 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.register); 21 22 final EditText et1=(EditText) findViewById(R.id.et1); 23 final EditText et2=(EditText) findViewById(R.id.et2); 24 Button btn=(Button) findViewById(R.id.btn1); 25 26 btn.setOnClickListener(new OnClickListener() { 27 28 @Override 29 public void onClick(View v) { 30 //获得主页面传过来的intent 31 Intent intent=getIntent(); 32 //获得输入的账户信息 33 String username=et1.getText().toString().trim(); 34 String password=et2.getText().toString().trim(); 35 //SharPreferences存储账户信息 36 SharedPreferences sp=getSharedPreferences("userinfo", Context.MODE_PRIVATE); 37 Editor editor=sp.edit(); 38 editor.putString("user", username); 39 editor.putString("password", password); 40 editor.commit(); 41 42 Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show(); 43 //跳回到登录页面 44 finish(); 45 } 46 }); 47 } 48 }
登录成功页面JAVA代码:
package com.qf.login; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class SuccessLogin extends Activity { Bitmap bmp; ImageView iv; String str_url = "http://pic1.cxtuku.com/00/09/47/b36872529f7c.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.success); // 获得intent对象 Intent intent = getIntent(); //获得存储在SharPreferences的账户信息 SharedPreferences sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE); iv = (ImageView) findViewById(R.id.iv); TextView tv = (TextView) findViewById(R.id.tv); tv.setText("欢迎" + sp.getString("user", "") + "登录" + "\n" + "您的密码是:" + sp.getString("password", "")); Button btn = (Button) findViewById(R.id.btn); //监听点击事件,联网请求异步加载图片 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //开启异步线程 MyBitmapTask task = new MyBitmapTask(); task.execute(str_url); task.setMyInterface(new MyInterface() { @Override public void getImageBitmap(Bitmap bmp) { // TODO Auto-generated method stub //写入到外部存储(SD卡) writeToOutStoragePublic(bmp); //从SD卡中读出图片并显示在屏幕 readFromOutStoragePublic(); } }); } }); } private void writeToOutStoragePublic(Bitmap bmp) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { File filepath = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); File file = new File(filepath, "dahai.jpg"); try { FileOutputStream fos = new FileOutputStream(file); bmp.compress(CompressFormat.JPEG, 60, fos); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private void readFromOutStoragePublic() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { File filepath = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); File file = new File(filepath, "dahai.jpg"); try { FileInputStream fis = new FileInputStream(file); bmp = BitmapFactory.decodeStream(fis); iv.setImageBitmap(bmp); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
自定义联网请求工具类代码:
1 package com.qf.login; 2 3 import java.io.InputStream; 4 import java.net.HttpURLConnection; 5 import java.net.URL; 6 7 import android.graphics.Bitmap; 8 import android.graphics.BitmapFactory; 9 10 public class HttpUtils { 11 12 public static Bitmap downloadImage(String str_url) { 13 Bitmap bmp = null; 14 try { 15 URL url = new URL(str_url); 16 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 17 InputStream is = conn.getInputStream(); 18 bmp = BitmapFactory.decodeStream(is); 19 } catch (Exception e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } 23 return bmp; 24 }; 25 26 }
自定义接口传值:
1 package com.qf.login; 2 3 import android.graphics.Bitmap; 4 5 public interface MyInterface { 6 void getImageBitmap(Bitmap bmp); 7 8 9 }
自定义Task类加载图片:
1 package com.qf.login; 2 3 import android.graphics.Bitmap; 4 import android.os.AsyncTask; 5 6 public class MyBitmapTask extends AsyncTask<String, Void, Bitmap> { 7 MyInterface myInterface; 8 9 10 public void setMyInterface(MyInterface myInterface) { 11 this.myInterface = myInterface; 12 } 13 @Override 14 protected Bitmap doInBackground(String... params) { 15 //加载图片 16 Bitmap bmp=HttpUtils.downloadImage(params[0]); 17 return bmp; 18 } 19 @Override 20 protected void onPostExecute(Bitmap result) { 21 // TODO Auto-generated method stub 22 super.onPostExecute(result); 23 //调用接口实现的方法 24 myInterface.getImageBitmap(result); 25 } 26 }
运行结果展示:
代码中若有不足欢迎留言建议!谢谢