目录
所遇到的问题
项目展示
登陆界面源码
TextView控件字体修改
忘记密码部分代码
打包apk
所遇到的问题
1、ImageView控件与EditView控件无法放置在同一行-----放置在水平线性布局里
2、Button按钮的背景色调整
android:background="@android:color/transparent" 背景色设置为透明
解决办法
找到项目中的这两个xml文件,将 parent="Theme.MaterialComponents.DayNight.DarkActionBar" 修改为 parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge" 即可解决,注意两个文件都要修改。若修改后没反应,可尝试关闭Android Studio并重新启动
3、界面之间的跳转Intent intent = new Intent(MainActivity.this,MainActivity2.class);//"MainActivity2.class"是所要跳转到的界面位置
startActivity(intent);
4、TextView字体的自定义
(1)下载所想要的字体文件(.TTF)
(2)Project--app--src--mian--右键Directory,命名为assets--再次新建文件夹fonts,然后将下载的字体文件放到fonts文件夹中
?(3)在对应的.java文件中进行字体代码修改
//获取控件
TextView wen = findViewById(R.id.wen);
//设置字体
Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/KaiTi.TTF");
//"fonts/KaiTi.TTF"是字体文件
wen.setTypeface(typeface);
5、界面之间的数据传递
(1)在所要传出数据的界面
//实例化窗口,并指定数据从哪个窗口传到哪个窗口
Intent intent = new Intent(MainActivity3.this, MainActivity.class);
//"MainActivity3.this"是当前窗口,"MainActivity.class"是获取数据的窗口
//将数据传到intent中
intent.putExtra("f_p", fix_pwd.getText().toString());
//开始跳转界面
startActivity(intent);
(2)接收数据界面
//获取intent对象
Intent intent = getIntent();
//将获取的数据赋给password
String password = intent.getStringExtra("f_p");
(6、做AS调取相机拍照加保存图像时,下面这行代码出现问题
path=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
报错是:Value must be ≥ 0
解决方法:把getColumnIndex换为getColumnIndexOrThrow可以运行
7、遇到“Cannot resolve?symbol”
解决方法:在AS中点击“File” -> “Invalidate Caches / Restart”,对话框选择?“Invalidate and Restart”,清空 cache 并且重启。)
项目展示
登陆界面源码
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//定义控件
private EditText name;
private EditText pwd;
private CheckBox show_pwd;
private Button forget_pwd;
private Button sign;
//定义用户名和密码
String user = "admin";
String password = "123456";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view();
even();
}
//控件实例化
public void view(){
name = findViewById(R.id.name);
pwd = findViewById(R.id.pwd);
show_pwd = findViewById(R.id.show_pwd);
forget_pwd = findViewById(R.id.forget_pwd);
sign = findViewById(R.id.sign);
}
//功能实现
public void even(){
//复选框的点击事件
//给CheckBox设置事件监听
show_pwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else {
pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
//按钮点击事件
//按钮1:忘记密码
forget_pwd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "点击重新设置密码", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,FixActivity.class);
startActivity(intent);
}
});
//按钮2:登录
sign.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String n = name.getText().toString();
String p = pwd.getText().toString();
boolean isFix = getIntent().getBooleanExtra("revise",false);
if (!isFix){
//判断用户名和密码
if (n.equals(user)&&p.equals(password)){
Toast.makeText(MainActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,TextActivity.class);
startActivity(intent);
}else if (n.equals("")||p.equals("")){
Toast.makeText(MainActivity.this, "用户名或密码不能为空!!!", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "用户名或密码错误!!!", Toast.LENGTH_SHORT).show();
}
}else {
String password2 = getIntent().getStringExtra("f_p");
//判断用户名和密码
if (n.equals(user)&&p.equals(password2)){
Toast.makeText(MainActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,TextActivity.class);
startActivity(intent);
}else if (n.equals("")||p.equals("")){
Toast.makeText(MainActivity.this, "用户名或密码不能为空!!!", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "用户名或密码错误!!!", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
TextView控件字体修改
package com.example.mytest2;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//获取控件
TextView wen = findViewById(R.id.wen);
//设置字体
Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/KaiTi.TTF");
wen.setTypeface(typeface);
}
}
忘记密码部分代码
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class FixActivity extends AppCompatActivity {
//定义控件
private EditText new_pwd;
private EditText fix_pwd;
private Button but_fix;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fix);
view();
even();
}
//控件实例化
public void view(){
new_pwd = findViewById(R.id.new_pwd);
fix_pwd = findViewById(R.id.fix_pwd);
but_fix = findViewById(R.id.but_fix);
}
//功能实现
public void even(){
but_fix.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String n_p = new_pwd.getText().toString();
String f_p = fix_pwd.getText().toString();
boolean isFix = true;
// if (n_p.equals(f_p)){
// Toast.makeText(MainActivity3.this, "密码修改成功!", Toast.LENGTH_SHORT).show();
// Intent intent = new Intent(MainActivity3.this,MainActivity.class);
// intent.putExtra("f_p",f_p);
// startActivity(intent);
// }else if (n_p.equals("")||f_p.equals("")){
// Toast.makeText(MainActivity3.this, "新密码不能为空!!!", Toast.LENGTH_SHORT).show();
// }else {
// Toast.makeText(MainActivity3.this, "两次密码不一致 !!!", Toast.LENGTH_SHORT).show();
// }
if (!n_p.equals("")&&!f_p.equals("")){
if (n_p.equals(f_p)) {
Toast.makeText(FixActivity.this, "密码修改成功!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(FixActivity.this, MainActivity.class);
intent.putExtra("f_p", f_p);
intent.putExtra("revise",isFix);
startActivity(intent);
}else{
Toast.makeText(FixActivity.this, "两次密码不一致 !!!", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(FixActivity.this, "新密码不能为空!!!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
打包apk
选择Build -> Generate Signed APK
?选择apk按钮,点击Next
新建一个签名文件,选择Create...,两个Password建议一样
点击Next后,选择在Build Variants中选择release,最后Finish
AS的右下角出现这个时成功
?
?
|