使用Intent

时间:2015-10-31 07:41:03

标签: java android android-studio

单击注册按钮,它必须将注册值注册到Register类,然后如果按下登录按钮,则必须将登录值注册到注册类。

MainActivity

public class MainActivity extends Activity {

private EditText main_edt_Mobile;
private EditText main_edt_Password;

private Button main_btn_Signup;
private Button main_btn_Login;

private String main_mobile;
private String main_password;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    main_edt_Mobile = (EditText) findViewById(R.id.edt_mobile);
    main_edt_Password = (EditText) findViewById(R.id.edt_password);

    main_btn_Login = (Button) findViewById(R.id.btn_login);
    main_btn_Signup = (Button) findViewById(R.id.btn_signup);

    main_btn_Login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "LOGED IN", Toast.LENGTH_SHORT).show();

            String main_mobile = main_edt_Mobile.getText().toString();
            String main_password = main_edt_Password.getText().toString();

            //Mobile
            if(main_mobile.length() == 10){

            }else{
                Toast.makeText(MainActivity.this, "Enter Only 10 Digit Number", Toast.LENGTH_SHORT).show();
                return;
            }
            String phone = String.valueOf(main_mobile);
            char c  = phone.charAt(0);
            if (c == '8' || c == '9' ||c =='7'){

            }else if( c == '0' ||c == '1' ||c == '2' ||c == '3' ||c == '4' ||c == '5' ||c == '6')
            {
                Toast.makeText(MainActivity.this, "Number Must Begin with 9 8 7",Toast.LENGTH_SHORT).show();
                return;
            }
            //Password
            if(main_password.length() <4){
                Toast.makeText(MainActivity.this, "Password Must Have Minimum 4 Character", Toast.LENGTH_SHORT).show();
                return;
            }else if(main_password.length()>=15){
                Toast.makeText(MainActivity.this, "Password Can Have Maximum 8 Character", Toast.LENGTH_SHORT).show();
                return;
            }


            Intent i = new Intent(MainActivity.this,Register.class );

            i.putExtra("key_main_m",main_mobile);
            i.putExtra("key_main_p",main_password);
            i.putExtra("Login",true);

            startActivity(i);

        }
    });
    main_btn_Signup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(MainActivity.this, "Opening Signup Page", Toast.LENGTH_SHORT).show();
            Intent i = new Intent(MainActivity.this,Signup.class );
            startActivity(i);
        }
    });
}}

注册活动

public class Signup extends Activity {

private String firstName ;
private String lastName ;
private String mobile;
private String password ;
private String email ;

private EditText edt_Signup_FirstName;
private EditText edt_Signup_LastName;
private EditText edt_Signup_Mobile;
private EditText edt_Signup_Password;
private EditText edt_Signup_Email;

private Button btn_Signup_Register;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signup);

    edt_Signup_FirstName=(EditText)findViewById(R.id.edt_signup_first_name);
    edt_Signup_LastName=(EditText)findViewById(R.id.edt_signup_last_name);
    edt_Signup_Mobile=(EditText)findViewById(R.id.edt_signup_mobile);
    edt_Signup_Password=(EditText)findViewById(R.id.edt_signup_password);
    edt_Signup_Email=(EditText)findViewById(R.id.edt_signup_email);

    btn_Signup_Register=(Button)findViewById(R.id.btn_signup_register);


    btn_Signup_Register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            firstName = edt_Signup_FirstName.getText().toString();
            String lastName = edt_Signup_LastName.getText().toString();
            String mobile = edt_Signup_Mobile.getText().toString();
            String password = edt_Signup_Password.getText().toString();
            String email = edt_Signup_Email.getText().toString();

            //First Name
            if(firstName.length() <8){
                Toast.makeText(Signup.this, "First Name Must Have Minimum 8 Character", Toast.LENGTH_SHORT).show();
                return;
            }else if(firstName.length()>=15){
                Toast.makeText(Signup.this, "First Name Can Have Maximum 8 Character", Toast.LENGTH_SHORT).show();
                return;
            }

            //Last Name
            if(lastName.length() <8){
                Toast.makeText(Signup.this, "Last Name Must Have Minimum 8 Character", Toast.LENGTH_SHORT).show();
                return;
            }else if(lastName.length()>=15){
                Toast.makeText(Signup.this, "Last Name Can Have Maximum 15 Character", Toast.LENGTH_SHORT).show();
                return;
            }
            //Mobile
            if(mobile.length() == 10){

            }else{
                Toast.makeText(Signup.this, "Enter Only 10 Digit Number", Toast.LENGTH_SHORT).show();
                return;
            }
            String phone = String.valueOf(mobile);
            char c  = phone.charAt(0);
            if (c == '8' || c == '9' ||c =='7'){

            }else if( c == '0' ||c == '1' ||c == '2' ||c == '3' ||c == '4' ||c == '5' ||c == '6')
            {
                Toast.makeText(Signup.this, "Number Must Begin with 9 8 7",Toast.LENGTH_SHORT).show();
                return;
            }
            //Password
            if(password.length() <4){
                Toast.makeText(Signup.this, "Password Must Have Minimum 4 Character", Toast.LENGTH_SHORT).show();
                return;
            }else if(password.length()>=15){
                Toast.makeText(Signup.this, "Password Can Have Maximum 8 Character", Toast.LENGTH_SHORT).show();
                return;
            }
            //Email
            if(email.length() <8){
                Toast.makeText(Signup.this, "Email Must Have Minimum 8 Character", Toast.LENGTH_SHORT).show();
                return;
            }else if(email.length()>=25){
                Toast.makeText(Signup.this, "Email Can Have Maximum 8 Character", Toast.LENGTH_SHORT).show();
                return;
            }

            Intent i =new Intent(Signup.this,Register.class);

            i.putExtra("key_f",firstName);
            i.putExtra("key_l",lastName);
            i.putExtra("key_m",mobile);
            i.putExtra("key_p",password);
            i.putExtra("key_e",email);

            startActivity(i);

        }
    });
}}

注册活动

public class Register extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    TextView txt_FirstName,txt_LastName,txt_Mobile,txt_Password,txt_Email;

    txt_Email =(TextView)findViewById(R.id.txt_email);
    txt_FirstName =(TextView)findViewById(R.id.txt_firstName);
    txt_LastName = (TextView)findViewById(R.id.txt_lastName);
    txt_Mobile = (TextView)findViewById(R.id.txt_mobile);
    txt_Password = (TextView)findViewById(R.id.txt_password);

    Intent i = getIntent();
    String main_mobile = i.getStringExtra("key_main_m");
    String main_password = i.getStringExtra("key_main_p");

    String firstname = i.getStringExtra("key_f");
    String lastname = i.getStringExtra("key_l");
    String mobile = i.getStringExtra("key_m");
    String password = i.getStringExtra("key_p");
    String email = i.getStringExtra("key_e");

    if ()
    {
        txt_Password.setText("PASSWORD : " +main_password);
        txt_Mobile.setText("MOBILE : " +main_mobile);
    }else if ()
    {
        txt_Email.setText("EMAIL : " +email);
        txt_FirstName.setText("FIRSTNAME : " +firstname);
        txt_LastName.setText("LASTNAME : " +lastname);
        txt_Mobile.setText("MOBILE : " +mobile);
        txt_Password.setText("PASSWORD : " +password);
    }
}}

要执行的代码单击Signup按钮,它必须将注册值设置为Register类,然后如果按下login按钮,则必须将Login值设置为注册类。

2 个答案:

答案 0 :(得分:0)

试试这个,

解析值

MainActivity.java

   public class MainActivity extends Activity implements OnClickListener {

    Button btn;
    TextView textBox;

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        btn = (Button) findViewById(R.id.btn);  
        textBox = (TextView)findViewById(R.id.address);
        btn.setOnClickListener(this);           
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn:
            String strTemp = "Test Test";
            textBox.setText(strTemp);

            Intent intent = new Intent(MainActivity.this, Second.class);
            intent.putExtra("tempstring", strTemp);
            startActivity(intent);
            break;

        default:
            break;
        }
    }
}

检索值

Second.java

 public class Second extends Activity {

    private TextView txtTemp;

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.second); 

        txtTemp = (TextView) findViewById(R.id.txt_temp);

        Intent intent = getIntent();
        String strTemp = intent.getStringExtra("tempstring");

        txtTemp.setText(strTemp);
    }
}

答案 1 :(得分:0)

您可以使用来自注册活动的intent.putExtra传递这些值

Intent i = new Intent(MainActivity.this,Register.class );    
i.putExtra("key_main_m",main_mobile);
i.putExtra("key_main_p",main_password);
i.putExtra("Login",true);

在您的登录活动onCreate中,您可以从Bundle中获取值,如下所示

Bundle extras = getIntent().getExtras();
String ph = extras.getString("key_main_m");
String pass = extras.getString("key_main_p");

并检查字符串中的值并执行任何操作。