第三周星期四每日总结
今日继续Android Studio的学习,今天学习了简单点页面间的跳转,能够使用按钮实现功能,通过代码控制其在各个页面间的跳转。
btnlogin.setOnClickListener(new View.OnClickListener() { //设置一个监听事件
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(index.this,Register.class); //跳转到所需页面
startActivity(intent);
Toast.makeText(index.this,"前往注册!",Toast.LENGTH_SHORT).show();//跳转提示
}
});
setContentView(R.layout.activity_index);//放在所需跳转页面,指示要跳转页面的.xml文件
第一次作业进度:写出了登录注册页面,欢迎页面,以及写作页面
代码文件
MainActivity.java:
1 package com.example.null001; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.Intent; 6 import android.content.SharedPreferences; 7 import android.database.Cursor; 8 import android.database.sqlite.SQLiteDatabase; 9 import android.os.Bundle; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.EditText; 13 import android.widget.Toast; 14 15 public class MainActivity extends AppCompatActivity { 16 EditText name,pwd; 17 Button btnlogin,btnreg; 18 Mysql mysql; 19 SQLiteDatabase db; 20 SharedPreferences sp1,sp2; 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 name = this.findViewById(R.id.name); //用户名输入框 26 pwd = this.findViewById(R.id.pwd); //密码输入框 27 btnlogin = this.findViewById(R.id.login); //登录按钮 28 btnreg = this.findViewById(R.id.reg); //注册按钮 29 sp1 = this.getSharedPreferences("useinfo",this.MODE_PRIVATE); 30 sp2 = this.getSharedPreferences("username",this.MODE_PRIVATE); 31 32 name.setText(sp1.getString("usname",null)); 33 pwd.setText(sp1.getString("uspwd",null)); 34 mysql = new Mysql(this,"Userinfo",null,1); //建数据库或者取数据库 35 db = mysql.getReadableDatabase(); 36 btnlogin.setOnClickListener(new View.OnClickListener() { //登录事件 37 @Override 38 public void onClick(View v) { 39 String username = name.getText().toString(); 40 String password = pwd.getText().toString(); //获取用户输入的用户名和密码 41 //查询用户名和密码相同的数据 42 Cursor cursor = db.query("logins",new String[]{"usname","uspwd"}," usname=? and uspwd=?",new String[]{username,password},null,null,null); 43 44 int flag = cursor.getCount(); //查询出来的记录项的条数,若没有该用户则为0条 45 if(flag!=0){ //若查询出的记录不为0,则进行跳转操作 46 Intent intent = new Intent(); 47 intent.setClass(MainActivity.this,Welcome.class); //设置页面跳转 48 SharedPreferences.Editor editor = sp2.edit(); 49 cursor.moveToFirst(); //将光标移动到position为0的位置,默认位置为-1 50 String loginname = cursor.getString(0); 51 editor.putString("Loginname",loginname); 52 editor.commit(); //将用户名存到SharedPreferences中 53 startActivity(intent); 54 } 55 else{ 56 Toast.makeText(MainActivity.this,"用户名或密码错误!",Toast.LENGTH_LONG).show(); //提示用户信息错误或没有账号 57 } 58 59 } 60 }); 61 62 btnreg.setOnClickListener(new View.OnClickListener() { //注册事件 63 @Override 64 public void onClick(View v) { 65 66 Intent intent = new Intent(); 67 intent.setClass(MainActivity.this,Register.class); //跳转到注册页面 68 startActivity(intent); 69 Toast.makeText(MainActivity.this,"前往注册!",Toast.LENGTH_SHORT).show(); 70 } 71 }); 72 } 73 }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.3" android:gravity="center" android:textSize="18dp" android:text="用户名:"/> <EditText android:id="@+id/name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginRight="20dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:textSize="18dp" android:layout_weight="0.3" android:text="密 码:"/> <EditText android:id="@+id/pwd" android:layout_width="0dp" android:layout_height="wrap_content" android:inputType="textPassword" android:layout_weight="1" android:layout_marginRight="20dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="5dp" android:text="登录" /> <Button android:id="@+id/reg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="5dp" android:text="注册" /> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
Welcome.java
package com.example.null001; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class Welcome extends AppCompatActivity { SharedPreferences sp; TextView showhello; Button btnlogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); sp = this.getSharedPreferences("username", this.MODE_PRIVATE); //获取sharepreferences showhello = this.findViewById(R.id.mainword); //显示欢迎 showhello.setText("欢迎你!"+sp.getString("Loginname","")); //获取用户名 btnlogin = this.findViewById(R.id.xie); btnlogin.setOnClickListener(new View.OnClickListener() { //注册事件 @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(Welcome.this,index.class); //跳转到注册页面 startActivity(intent); Toast.makeText(Welcome.this,"前往发布!",Toast.LENGTH_SHORT).show(); } }); } }
activity_welcome.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".Welcome"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="0dp" android:layout_height="0dp" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="400dp" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@+id/linearLayout"> <TextView android:id="@+id/mainword" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:textSize="22dp" tools:ignore="MissingConstraints" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/xie" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="5dp" android:text="写入" /> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
index.java
package com.example.null001; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class index extends AppCompatActivity { EditText biao, zheng; Button btnlogin; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_index); biao = this.findViewById(R.id.name); //用户名输入框 zheng = this.findViewById(R.id.pwd); //密码输入框 btnlogin = this.findViewById(R.id.submit); //登录按钮 btnlogin.setOnClickListener(new View.OnClickListener() { //注册事件 @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(index.this,Register.class); //跳转到注册页面 startActivity(intent); Toast.makeText(index.this,"前往注册!",Toast.LENGTH_SHORT).show(); } }); } }
activity_index.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".Register"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 用户名部分 --> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="标题:" /> <EditText android:id="@+id/biao" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="" android:text="" /> <!-- 密码部分 --> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="内容:" /> <EditText android:id="@+id/zheng" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPassword" /> <Button android:id="@+id/submit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="发布" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
以上为登录,欢迎,发布页面的代码.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通