今天继续写了注册功能,尝试加入在手机相册自选头像或拍照上传头像功能
页面
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<!--使用线性布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#F5F5F5"
android:gravity="center_horizontal">
<!--Logo-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="30dp" >
<ImageView
android:id="@+id/iv_personal_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/default_personal_image" />
</RelativeLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_change"
android:layout_marginTop="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择头像" >
</androidx.appcompat.widget.AppCompatButton>
<!--嵌套线性布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--嵌套线性布局-->
<LinearLayout
android:id="@+id/UserNameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--用户名输入-->
<EditText
android:id="@+id/UserNameEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="15dp"
android:background="@drawable/translucent_edit"
android:hint="输入用户名"
android:textSize="24dp"
android:singleLine="true" />
</LinearLayout>
<!--嵌套线性布局-->
<LinearLayout
android:id="@+id/PassWordLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--密码输入-->
<EditText
android:id="@+id/PassWordEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="15dp"
android:background="@drawable/translucent_edit"
android:hint="输入用户密码"
android:textSize="24dp"
android:maxLength="16"
android:singleLine="true"
android:inputType="textPassword" />
</LinearLayout>
<!--嵌套线性布局-->
<LinearLayout
android:id="@+id/LayoutButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--注册按钮-->
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/SignUpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_margin="15dp"
android:layout_weight="1"
android:textColor="@color/white"
android:background="@drawable/translucent_button"
android:text="注 册"
android:textSize="24dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
功能代码
package com.example.share;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
//import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import com.example.share.R;
import com.example.share.dao.UserDao;
import com.example.share.entity.MyUser;
import com.example.share.entity.User;
import com.example.share.utils.CommonUtils;
public class register extends AppCompatActivity implements View.OnClickListener {
private Button SignUpButton,btn_change;
private EditText UserNameEdit,PassWordEdit;
private Handler mainHandler;
private UserDao userDao;
protected static final int CHOOSE_PICTURE = 0;
protected static final int TAKE_PICTURE = 1;
private static final int CROP_SMALL_PICTURE = 2;
protected static Uri tempUri;
private ImageView iv_personal_icon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// EdgeToEdge.enable(this);
setContentView(R.layout.activity_register);
mainHandler=new Handler(getMainLooper());
userDao=new UserDao();
SignUpButton = findViewById(R.id.SignUpButton);
btn_change = findViewById(R.id.btn_change);
UserNameEdit = findViewById(R.id.UserNameEdit);
PassWordEdit = findViewById(R.id.PassWordEdit);
SignUpButton.setOnClickListener(this);
btn_change.setOnClickListener(this);
iv_personal_icon =findViewById(R.id.iv_personal_icon);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.SignUpButton) {
String name=UserNameEdit.getText().toString().trim();
String password=PassWordEdit.getText().toString().trim();
new Thread(new Runnable() {
@Override
public void run() {
MyUser myUser =new MyUser(name,password);
int iRow=0;
iRow=userDao.addMyUser(myUser);
Intent intent=new Intent(register.this, register.class);
Bundle bundle=new Bundle();
bundle.putString("name",name);
intent.putExtras(bundle);
startActivity(intent);
if(iRow>0){
mainHandler.post(new Runnable() {
@Override
public void run() {
CommonUtils.showShortMsg(register.this,"注册成功");
}
});
}
else{
mainHandler.post(new Runnable() {
@Override
public void run() {
CommonUtils.showShortMsg(register.this,"注册失败");
Intent intent=new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
}
}
}).start();
}
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
super.onPointerCaptureChanged(hasCapture);
}
}