很高兴再次和大家分享Android的相关知识,这次主要就是和大家分享Android中Activity之间的数据传输,记录一下这段时间学习的知识,如有错误,还请大家指正。
效果呈现
以上是做出来的大概效果,中间有一段是黑屏的是在输入密码,这个不是故意要设置成黑屏的,只是莫名其妙就这样了。接下来我们就来讲解一下制作的原理。
布局界面代码和逻辑代码
以下是布局界面的代码,也没有什么参考价值,在我上一篇讲解登录界面的时候用到过,不过这里的第二个界面我用到了约束布局,这种布局用起来会很方便,约束布局就是我们建立一个布局文件时默认的一个布局,个人感觉用起来时很好的,大家后期做项目的时候可以用这个。
<!--main界面的code-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="200dp"
android:drawableLeft="@drawable/head"
android:hint="用户名"
android:inputType="text"
></EditText>
<EditText
android:id="@+id/e2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/e1"
android:layout_marginTop="40dp"
android:drawableLeft="@drawable/password"
android:hint="密码"
android:inputType="numberPassword"></EditText>
/*对按钮的一些文件设置不起作用*/
<Button
android:id="@+id/btn1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@id/e2"
android:layout_centerInParent="true"
android:layout_marginTop="60dp"
android:foreground="@drawable/simplesetting"
android:text="登录" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn1"
android:layout_marginTop="20dp"
android:layout_centerInParent="true"
android:text="点击跳转到新世界"
></Button>
</RelativeLayout>
<?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=".change">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="登录成功,你的密码为:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></TextView>
<TextView
android:id="@+id/T1"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginStart="60dp"
android:layout_marginTop="176dp"
android:text=""
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></TextView>
<TextView
android:id="@+id/T2"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginTop="40dp"
android:text=""
app:layout_constraintEnd_toEndOf="@+id/T1"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/T1"
app:layout_constraintTop_toBottomOf="@+id/T1"></TextView>
</androidx.constraintlayout.widget.ConstraintLayout>
java代码:按顺序依次是:main界面的,change界面的。
package com.unity3d.login;
import static android.widget.Toast.*;
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 MainActivity extends AppCompatActivity {
public Button btn1;
public EditText E1;
public EditText E2;
public Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=findViewById(R.id.btn1);
E1=findViewById(R.id.e1);//获取用户名控件
E2=findViewById(R.id.e2);//获取密码控件对象
btn2=findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name=E1.getText().toString().trim();//trim()用于删除字符串中的空格包括空格,Tab键、换行符等。
String password=E2.getText().toString().trim();
if(name.equals("oyyf")&&password.equals("123456")){//判断用户名输入的name是否为“oyyf”,密码输入是否为“123456”
//错误示范if(name=="oyyf"&&password=="123456")
Toast.makeText(getApplicationContext(), "登录成功", LENGTH_SHORT).show();
Intent intent=new Intent(MainActivity.this,change.class);
intent.putExtra("n",name);//将name传送到要跳转的界面,这里要注意是以n这个名字传送的。
intent.putExtra("p",password);
startActivity(intent);
}
else{
Toast.makeText(getApplicationContext(),"登录失败", LENGTH_SHORT).show();
}
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent ii=new Intent(MainActivity.this,Strongnan.class);
startActivity(ii);
}
});
}
}
package com.unity3d.login;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class change extends AppCompatActivity {
public TextView T1;
public TextView T2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change);
T1=findViewById(R.id.T1);
T2=findViewById(R.id.T2);
Intent intent=getIntent();//用来接收别的界面传来的信息。
String name=intent.getStringExtra("n");//接收名字为n的里面的信息。
String password=intent.getStringExtra("p");
T1.setText("name:"+name);//将T1控件的文本显示用户名,根据上个界面传送过来的。
T2.setText("password:"+password);
}
}
核心思想
在Activity之间传输数据用Intent,将数据放在这个里面然后当设置页面跳转的时候将里面的数据一并传输过去,在接受数据的Activity里面设置接受函数接受数据,详情请看注解。
小结
看了实例之后会不会觉得很简单呢?这个在我们的APP设计的时候用的还是挺多的,当然后面肯定是还要连接数据库的,这只是一点点皮毛而已,希望大家能够和我一起坚持学习下去,相信会有很多收获的。最后,送给大家一句话,慢慢来,比较快!!