Android based intent data transmission

Keywords: xml Java less Android

Step 1: LoginActivity.xml

The interface is relatively simple, so there is no need to paste the content here, and the renderings are pasted here.  

2.LoginActivity.java

The logic processing code is as follows:

public class LoginActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    private Button btn_login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        et_username=findViewById(R.id.et_username);
        et_password=findViewById(R.id.et_password);
        btn_login=findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username=et_username.getText().toString().trim();
                String password=et_password.getText().toString().trim();
                if (username.isEmpty()||password.isEmpty())
                {
                    Toast.makeText(LoginActivity.this,"username or password is null",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!username.equals("123")||!password.equals("123"))
                {
                    Toast.makeText(LoginActivity.this,"username or password is error",Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent=new Intent();
                Bundle bundle=new Bundle();
                bundle.putString("username",username);
                bundle.putString("password",password);
                intent.putExtras(bundle);
                intent.setClass(LoginActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

Simple description: get the control corresponding to username and password from xml, and set the click event listener View.OnClickListener() of BTN login button. After clicking, get the input content and judge. Only when username and password are "123" at the same time, can they pass the verification, package with bundle (or not when there is less data) and start intent.

3.MainAcctivity.java

public class MainActivity extends AppCompatActivity {
    private TextView tv_data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_data=findViewById(R.id.tv_data);
        Bundle bundle=getIntent().getExtras();
        String data=bundle.getString("username")+bundle.getString("password");
        tv_data.setText(data);
    }
}
//Success, self-study in Android... If you have any questions, please correct

 

Posted by harnacks on Mon, 16 Dec 2019 12:21:24 -0800