guozi6

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

完成了个人界面的编辑,同时可以编辑个人资料

package com.hui.testend.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Looper;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hui.testend.R;
import com.hui.testend.loginActivity;
import com.hui.testend.myself.editself;
import com.hui.testend.myself.selfitem;
import com.hui.testend.mytool.Dao;
import com.hui.testend.mytool.ImageUtil;

import java.util.Arrays;
import java.util.Base64;
import java.util.Objects;

/**
* A simple {@link Fragment} subclass.
* Use the {@link Fragment_self#newInstance} factory method to
* create an instance of this fragment.
*/
public class Fragment_self extends Fragment implements View.OnClickListener {
Button btn_edit_self,btn_destroy_login;
ImageView iv_avatar;
TextView tv_self_name,tv_account_text,tv_sex,tv_phone,tv_sign;
Handler mainhandler=new Handler(Looper.getMainLooper());

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

public Fragment_self() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Fragment_self.
*/
// TODO: Rename and change types and number of parameters
public static Fragment_self newInstance(String param1, String param2) {
Fragment_self fragment = new Fragment_self();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_self, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init(view);
initdata();
}
public void init(View view){
btn_edit_self=view.findViewById(R.id.btn_edit_self);
btn_destroy_login=view.findViewById(R.id.btn_destroy_login);
iv_avatar=view.findViewById(R.id.iv_avatar);
tv_self_name=view.findViewById(R.id.tv_self_name);
tv_account_text=view.findViewById(R.id.tv_account_text);
tv_sex=view.findViewById(R.id.tv_sex);
tv_phone=view.findViewById(R.id.tv_phone);
tv_sign=view.findViewById(R.id.tv_sign);
btn_edit_self.setOnClickListener(this);
btn_destroy_login.setOnClickListener(this);
}
public void initdata(){
new Thread(new Runnable() {
@Override
public void run() {
SharedPreferences spf=getActivity().getSharedPreferences("Spf",Context.MODE_PRIVATE);
Dao dao=new Dao();
selfitem self=new selfitem();
self=dao.getself(spf.getString("name","张三"));
selfitem finalSelf = self;
mainhandler.post(new Runnable() {
@Override
public void run() {
System.out.println(99989898);
tv_sex.setText(finalSelf.getSex());
tv_account_text.setText(finalSelf.getName());
tv_self_name.setText(finalSelf.getName());
System.out.println(2623231);
tv_phone.setText(finalSelf.getPhone());
tv_sign.setText(finalSelf.getSign());
System.out.println(13246);
// iv_avatar.setImageBitmap(null);
iv_avatar.setImageBitmap(ImageUtil.base64ToImage(Base64.getEncoder().encodeToString(finalSelf.getImg())));
System.out.println(215494);
}
});
}
}).start();
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_edit_self:
System.out.println(1111);
Intent intent=new Intent(getActivity(), editself.class);
startActivity(intent);
break;
case R.id.btn_destroy_login:
SharedPreferences preferences= requireActivity().getSharedPreferences("Spf", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= preferences.edit();
editor.putBoolean("remember_login",false);
editor.commit();
Intent intent1=new Intent(getActivity(), loginActivity.class);
startActivity(intent1);
break;
}
}
}
package com.hui.testend.myself;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.text.Selection;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.hui.testend.R;
import com.hui.testend.list.ItemBean;
import com.hui.testend.list.MyAdapter;
import com.hui.testend.mytool.Dao;
import com.hui.testend.mytool.ImageUtil;
import com.hui.testend.recongnize.MyObject;
import com.hui.testend.recongnize.altest;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class editself extends AppCompatActivity implements View.OnClickListener{
EditText ed_self_name,ed_self_phone,ed_self_sign;
Button take_photo,choose_photo,save;
RadioButton rb_boy,rb_girl;
public int REQUEST_CODE_TAKE =1,REQUEST_CODE_CHOOSE=0;
Uri imageUri;
Handler mainhandler;
ImageView iv;
Bitmap temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editself);
init();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1:
if (resultCode==RESULT_OK){
InputStream inputStream = null;
try {
inputStream = getContentResolver().openInputStream(imageUri);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
iv.setImageBitmap(bitmap);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); // 100表示压缩质量
byte[] byteArray = baos.toByteArray();
new Thread(new Runnable() {
@Override
public void run() {
altest altest=new altest();
MyObject a=altest.getdata1(byteArray);
mainhandler.post(new Runnable() {
@Override
public void run() {
try {
InputStream inputStream = getContentResolver().openInputStream(imageUri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
iv.setImageBitmap(bitmap);
temp=bitmap;
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
});
}
}).start();
}
break;
case 0:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
imageUri=uri;
String imagePath = getPathFromUri(uri);
new Thread(new Runnable() {
@Override
public void run() {
altest altest = new altest();
MyObject a=altest.getdata(imagePath);
mainhandler.post(new Runnable() {
@Override
public void run() {
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
temp=bitmap;
iv.setImageBitmap(bitmap);
}
});
}
}).start();
break;
}
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 权限被授予,可以进行相应的操作
// 在这里进行访问文件的操作
doTake();
} else {
Toast.makeText(this, "需要读取外部存储权限才能访问文件", Toast.LENGTH_SHORT).show();
}
break;
case 0:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 权限被授予,可以进行相应的操作
// 在这里进行访问文件的操作
dochoose();
} else {
Toast.makeText(this, "需要读取外部存储权限才能访问文件", Toast.LENGTH_SHORT).show();
}
break;
}
}

private void dochoose() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_CHOOSE);
}
private void doTake() {
File imageTemp = new File(getExternalCacheDir(), "imageOut.jpeg");
if (imageTemp.exists()) {
imageTemp.delete();
}
try {
imageTemp.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

if (Build.VERSION.SDK_INT > 24) {
// contentProvider
imageUri = FileProvider.getUriForFile(this, "com.hui.testend.fileprovider", imageTemp);
} else {
imageUri = Uri.fromFile(imageTemp);
}
Intent intent = new Intent();
intent.setAction("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, REQUEST_CODE_TAKE);
}
public void choosePhoto(View view){
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// 真正的执行去相册
dochoose();
} else {
// 去申请权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}
}
public void takePhoto(View view) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
// 真正的执行去拍照
doTake();
} else {
// 去申请权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
}
}
private String getPathFromUri(Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
cursor.close();
return path;
}
public void init(){
ed_self_phone=findViewById(R.id.ed_self_phone);
ed_self_name=findViewById(R.id.ed_self_name);
ed_self_sign=findViewById(R.id.ed_self_sign);
take_photo=findViewById(R.id.take_photo_self);
choose_photo=findViewById(R.id.choose_photo_self);
rb_boy=findViewById(R.id.rb_boy);
rb_girl=findViewById(R.id.rb_girl);
save=findViewById(R.id.save);
iv=findViewById(R.id.iv_head);
take_photo.setOnClickListener(this);
choose_photo.setOnClickListener(this);
mainhandler=new Handler(getMainLooper());
save.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.take_photo_self:
takePhoto(v);
break;
case R.id.choose_photo_self:
System.out.println(99999990);
choosePhoto(v);
break;
case R.id.save:
updateself();
break;
}
}
public void updateself(){
SharedPreferences spf=getSharedPreferences("Spf",MODE_PRIVATE);
String name1=spf.getString("name","张三");
String name=ed_self_name.getText().toString().trim();
String phone=ed_self_phone.getText().toString().trim();
String sign=ed_self_sign.getText().toString().trim();
String sex="";
if (rb_boy.isChecked()){
sex="男";
}
else{
sex="女";
}
String finalSex = sex;
new Thread(new Runnable() {
@Override
public void run() {
Dao dao=new Dao();
dao.updateself(ImageUtil.imageToBase64(temp),name,finalSex,phone,sign,name1);
mainhandler.post(new Runnable() {
@Override
public void run() {
finish();
}
});
}
}).start();

}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".fragment.Fragment_self">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
>

<ImageView
android:id="@+id/iv_avatar"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher_round" />

<TextView
android:id="@+id/tv_self_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv_avatar"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="fxjzzyo"
android:textColor="@color/white"
android:textSize="14sp" />


</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:paddingLeft="10dp">

<TextView
android:id="@+id/tv_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
android:textSize="20sp" />

<TextView
android:id="@+id/tv_account_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="123234"

android:textSize="20sp" />
</RelativeLayout>


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:paddingLeft="10dp">

<TextView
android:id="@+id/tv_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="性别"
android:textSize="20sp" />

<TextView
android:id="@+id/tv_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="男"
android:textSize="20sp" />
</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:paddingLeft="10dp">

<TextView
android:id="@+id/tv_school"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="电话"
android:textSize="20sp" />

<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text=""
android:textSize="20sp" />
</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:paddingLeft="10dp">

<TextView
android:id="@+id/tv_sign_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="个人签名"
android:textSize="20sp" />

<TextView
android:id="@+id/tv_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="这个人没有留下姓名"
android:textSize="14sp" />
</RelativeLayout>

<Button
android:id="@+id/btn_edit_self"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="编辑资料" />

<Button
android:id="@+id/btn_destroy_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="退出登录" />
</LinearLayout>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".myself.editself">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="更换头像"
android:textSize="20sp" />

<ImageView
android:id="@+id/iv_head"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher_round" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/iv_head"
android:layout_marginTop="5dp"
android:gravity="center"
android:orientation="horizontal">

<Button
android:id="@+id/take_photo_self"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍照" />

<Button
android:id="@+id/choose_photo_self"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="相册" />

</LinearLayout>

</RelativeLayout>


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:paddingLeft="10dp">

<TextView
android:id="@+id/tv_self_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="昵称"
android:textSize="20sp" />

<EditText
android:id="@+id/ed_self_name"
android:layout_height="40sp"
android:layout_width="match_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/tv_self_name"
android:textSize="15sp" />
</RelativeLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center_vertical"
android:orientation="horizontal">

<TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="性别"
android:textSize="20sp" />

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">

<RadioButton
android:id="@+id/rb_boy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />

<RadioButton
android:id="@+id/rb_girl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="女" />
</RadioGroup>

</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:paddingLeft="10dp">

<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="电话"
android:textSize="20sp" />

<EditText
android:id="@+id/ed_self_phone"
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/tv_phone"
android:gravity="center"
android:text="北京大学"
android:textSize="15sp" android:layout_height="40dp"/>
</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="10dp">

<TextView
android:id="@+id/tv_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="个人签名"
android:textSize="20sp" />

<EditText
android:id="@+id/ed_self_sign"
android:layout_width="match_parent"
android:layout_below="@id/tv_sign"
android:layout_marginTop="5dp"
android:gravity="start|top"
android:minHeight="200dp"
android:paddingLeft="10dp"
android:text="这个人没有留下姓名"
android:textSize="16sp" android:layout_height="40dp"/>
</RelativeLayout>

<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="保存" />
</LinearLayout>
</ScrollView>
posted on 2024-05-08 23:11  汀幻  阅读(3)  评论(0编辑  收藏  举报