三月十一日
今天完成了button按钮的跳转,其具体步骤为声明控件---找到控件---实现跳转和匹配对应用户名和密码,获取edittest里面的账号和密码与规定的进行匹配成功则进行跳转。
首先声明控件private Button mBtnLogin;然后找到控件mBtnLogin=findViewById(R.id.btn_login);
最后实现跳转//实现直接跳转---方法一
mBtnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=null;//用intent来放目的跳转的文件
intent =new Intent(MainActivity.this,functionActivity.class);
//用一个函数或方法去实现他
startActivities(intent);
}
});
intent =new Intent(MainActivity.this,functionActivity.class);中MainActivity为目前文件,functionActivity为目标文件。
*****获取edittest里面的账号和密码*****
1.首先需要声明控件
private EditText mEtuser;和
private EditText mEtpassword;
2.获取输入的用户名和密码
String username=mEtuser.getText().toString();
String userpassword=mEtpassword.getText().toString();
其中mEtuser.getText()yong用来获取,toString()用来转换为字符
3.用if else语句来确定对错
if(username.equals("nyf")&&userpassword.equals("123465"))
{
//如果正确的话进行跳转
intent = new Intent(MainActivity.this,functionActivity.class);
startActivity(intent);
}else{
//不正确,弹出登录失败toast
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | package com.example.firstapp; 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; public class MainActivity extends AppCompatActivity implements View.OnClickListener { //声明控件 private Button mBtnLogin; //获取用户名和密码用 private EditText mEtuser; private EditText mEtpassword; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到控件 mBtnLogin=findViewById(R.id.btn_login); mEtuser=findViewById(R.id.et_1); mEtpassword=findViewById(R.id.et_2); /* //实现直接跳转---方法一 mBtnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=null;//用intent来放目的跳转的文件 intent =new Intent(MainActivity.this,functionActivity.class); //用一个函数或方法去实现他 startActivities(intent); } }); */ //匹配对应的用户名和密码才能进行登录操作 mBtnLogin.setOnClickListener( this ); } public void onClick(View v) { //需要获取输入的用户名和密码 String username=mEtuser.getText().toString(); String userpassword=mEtpassword.getText().toString(); Intent intent = null ; //假设正确的账号和密码是nyf和123456 if (username.equals( "nyf" )&&userpassword.equals( "123465" )) { //如果正确的话进行跳转 intent = new Intent(MainActivity. this ,functionActivity. class ); startActivity(intent); } else { //不正确,弹出登录失败toast } } @Override public void onPointerCaptureChanged( boolean hasCapture) { super .onPointerCaptureChanged(hasCapture); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout 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= "horizontal" android:padding= "10dp" tools:context= ".functionActivity" > <TextView android:id= "@+id/tv_func_1" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "跳转后界面" android:textSize= "30dp" android:gravity= "center" android:textColor= "@color/black" /> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout 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" android:background= "@drawable/background" tools:context= ".MainActivity" > <TextView android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "每日登陆打卡" android:textSize= "35dp" android:gravity= "center" android:layout_marginTop= "50dp" app:layout_constraintBottom_toBottomOf= "parent" app:layout_constraintEnd_toEndOf= "parent" app:layout_constraintStart_toStartOf= "parent" app:layout_constraintTop_toTopOf= "parent" /> <EditText android:id= "@+id/et_1" android:layout_width= "match_parent" android:layout_height= "50dp" android:textSize= "25dp" android:textColor= "@color/black" android:hint= "用户名" android:maxLines= "1" android:padding= "10dp" android:background= "@drawable/bg_username" /> <EditText android:id= "@+id/et_2" android:layout_width= "match_parent" android:layout_height= "50dp" android:textSize= "25dp" android:textColor= "@color/black" android:hint= "密码" android:maxLines= "1" android:padding= "10dp" android:layout_marginTop= "10dp" android:inputType= "textPassword" android:background= "@drawable/bg_username" /> <LinearLayout android:layout_width= "match_parent" android:layout_height= "wrap_content" android:orientation= "horizontal" android:layout_marginTop= "20dp" > <Button android:id= "@+id/btn_login" android:layout_width= "0dp" android:layout_weight= "1" android:layout_height= "wrap_content" android:text= "登录" android:textSize= "20dp" android:layout_marginLeft= "20dp" android:layout_gravity= "center" /> <Button android:layout_width= "0dp" android:layout_height= "wrap_content" android:layout_weight= "1" android:text= "注册" android:layout_marginLeft= "20dp" android:layout_marginRight= "20dp" /> </LinearLayout> </LinearLayout> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署