今天我实现了欢迎页面和登录页面的页面布局和功能。
(1)欢迎页面布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <ImageView android:id="@+id/ivlogin" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerVertical="true" android:src="@drawable/splash_default" /> <Button android:id="@+id/btn_begin" android:layout_width="wrap_content" android:layout_height="46dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="30dp" android:background="@drawable/jiantou2" android:text="@string/login_btn_begin" /> </RelativeLayout>
(2)欢迎页面的功能实现
package com.example.personmoney; import java.util.Calendar; import javax.security.auth.PrivateCredentialPermission; import com.example.feigou.activity.MainActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.format.Time; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.Button; import android.widget.ImageView; public class LoginActivity extends Activity { private ImageView loginImage; private Button loginBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); } public void initview() { loginImage=(ImageView) findViewById(R.id.ivlogin); loginBtn=(Button) findViewById(R.id.btn_begin); //简便动画效果 Animation alphaAnimation=new AlphaAnimation(0.1f, 1.0f); alphaAnimation.setDuration(3000);//设置动画持续时间 alphaAnimation.setFillAfter(true);//设置动画结束后保持当前的位置 //将动画效果添加到图片中 loginImage.startAnimation(alphaAnimation); loginBtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(LoginActivity.this, LoginActivity2.class); startActivity(intent); LoginActivity.this.finish(); } }); } }
(3)登录页面的页面布局
<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:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.login.MainActivity" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_gravity="center_horizontal" android:src="@drawable/xiaohu" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_gravity="center_horizontal" android:src="@drawable/pingguo" /> <EditText android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户名" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_gravity="center_horizontal" android:src="@drawable/pingguo" /> <EditText android:id="@+id/et_userpassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:password="true" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="20dp" > <CheckBox android:id="@+id/cb_ischeck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住用户名密码" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:onClick="login" android:text="登录" /> </RelativeLayout> </LinearLayout>
(4)登录页面的功能实现
package com.example.personmoney; import java.util.Map; import com.example.feigou.activity.MainActivity; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.text.TextUtils; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class LoginActivity2 extends Activity { private EditText et_name; private EditText et_userpassword; private CheckBox cb_ischeck; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login2); //1.找到我们关心的控件 et_name=(EditText)findViewById(R.id.et_username); et_userpassword=(EditText)findViewById(R.id.et_userpassword); cb_ischeck=(CheckBox)findViewById(R.id.cb_ischeck); //在config.xml文件中把我们关心的数据取出来 然后显示在Edittext控件上 //使用SharedPreferences 去保存数据 那到sp的实例 SharedPreferences sPreferences=getSharedPreferences("config", 0); String name=sPreferences.getString("name", ""); String password=sPreferences.getString("pwd", ""); boolean status=sPreferences.getBoolean("status", false); et_name.setText(name); et_userpassword.setText(password); cb_ischeck.setChecked(status); } //写点击事件 public void login(View v) { //2.1获取用户名和密码SS String name=et_name.getText().toString().trim(); String password=et_userpassword.getText().toString().trim(); System.out.println("cb_ischeck:"+cb_ischeck.isChecked()); //2.2判断name和password是否为空 if(TextUtils.isEmpty(name)||TextUtils.isEmpty(password)) { Toast.makeText(LoginActivity2.this, "用户名和密码不能为空", 1).show(); } else { //进行登录的逻辑 Intent intent=new Intent(); intent.setClass(LoginActivity2.this, MainActivity.class); startActivity(intent); if(cb_ischeck.isChecked()) { //使用SharedPreferences 去保存数据 那到sp的实例 SharedPreferences sPreferences=getSharedPreferences("config", 0); //获取sp的编辑器 Editor editor=sPreferences.edit(); editor.putString("name", name); editor.putString("pwd", password); editor.putBoolean("status", true); //提交 editor.commit(); } else { Toast.makeText(LoginActivity2.this, "请勾选'记住用户名密码'", 1).show(); } } } }
(5)软件设置的功能实现
package com.example.feigou.test; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import com.example.personmoney.R; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.provider.MediaStore.Audio.Media; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class SettingActivity extends Activity implements OnClickListener { private ImageView headImage; private TextView tvNickName; private TextView tvWeiXinNum; private Button setting_photo; private ImageView ivHead;//头像显示 private Button btnTakephoto;//拍照 private Button btnphoto;//相册 private Bitmap head;//头像 private static String path="storage/emulated/DCIM/Camera/";//存放照片路径 @Override protected void onCreate(Bundle savedInstanceState) { /** * 方法的一句话概述 * 方法详述(简单方法可不必详述) * @param s 说明参数含义 * @return 说明返回值含义 * @throws IOException 说明发生此异常的条件 * @throws NullPointerException 说明发生此异常的条件 */ super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting); init(); } public void init() { setting_photo = (Button) findViewById(R.id.setting_photo); setting_photo.setOnClickListener(this); headImage = (ImageView) findViewById(R.id.main_me_headphoto); //findViewById(R.id.main_me_setting).setOnClickListener(this); findViewById(R.id.main_me_expression).setOnClickListener(this);// 表情 findViewById(R.id.main_me_collect).setOnClickListener(this);// 收藏 //设置头像 btnphoto=(Button) findViewById(R.id.btn_photo); btnTakephoto=(Button) findViewById(R.id.btn_takephoto); ivHead=(ImageView) findViewById(R.id.main_me_headphoto); //从Sd卡中找头像,转换成Bitmap Bitmap bt=BitmapFactory.decodeFile(path+"head.jpg"); if(bt!=null) { Drawable drawable=new BitmapDrawable(bt);//转换成drawerable ivHead.setImageDrawable(drawable); }else {//如果没有照片再从服务器取头像,存储取回来的头像 } } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.setting_photo: { Toast.makeText(this, "进入圖片頁面,暂时还没实现", Toast.LENGTH_SHORT).show(); break; } case R.id.main_me_collect: { Toast.makeText(this, "进入收藏界面,暂时还没实现", Toast.LENGTH_SHORT).show(); break; } case R.id.main_me_expression: { Toast.makeText(this, "进入表情界面,暂时还没实现", Toast.LENGTH_SHORT).show(); break; } case R.id.btn_photo:{//从相册里取照片 Intent intent1=new Intent(Intent.ACTION_PICK,null); intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "imag/*"); startActivityForResult(intent1, 1); break; } case R.id.btn_takephoto:{//从相册里取照片 Intent intent2=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent2.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"head.jpg"))); startActivityForResult(intent2, 2); break; } /*case R.id.main_me_setting: { //可以修改自己的頭像 Toast.makeText(this, "进入個人設置界面,暂时还没实现", Toast.LENGTH_SHORT).show(); break; }*/ } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub switch(requestCode) { case 1: if(resultCode==RESULT_OK) { cropPhoto(data.getData());//裁剪图片 } break; case 2: if(resultCode==RESULT_OK) { File temp=new File(Environment.getExternalStorageDirectory()+"/head.jpg"); cropPhoto(Uri.fromFile(temp));//裁剪图片 } break; case 3: if(data!=null) { Bundle extras=data.getExtras(); head=extras.getParcelable("data"); if(headImage!=null) { //上传服务器 setPicToView(head);//保存在SD卡中 ivHead.setImageBitmap(head);//用ImageView 显示出来 } } break; default: break; } super.onActivityResult(requestCode, resultCode, data); } //调用系统的裁剪 public void cropPhoto(Uri uri) { Intent intent=new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "imag/*"); intent.putExtra("crop", "true"); //aspectX aspectY是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); //outputX outputY是裁剪图片宽高 intent.putExtra("outputX", 150); intent.putExtra("outputY", 150); intent.putExtra("return_data", true); startActivityForResult(intent, 3); } //保存在SD卡 private void setPicToView(Bitmap mBitmap) { String sdStatus=Environment.getExternalStorageState(); if(!sdStatus.equals(Environment.MEDIA_MOUNTED)) {//检测sd是否可用 return; } FileOutputStream b=null; File file=new File(path); file.mkdirs();//创建文件夹 String fileName=path+"head.jpg";//图片名字 try { b=new FileOutputStream(fileName); mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b); }catch(FileNotFoundException e) { e.printStackTrace(); }finally { try { //关闭流 b.flush(); b.close(); }catch(IOException e){ e.printStackTrace(); } } } }