今天看啥
热点:

Intent的主要作用,Intent主要作用


Intent

意图

Intent在Android系统的作用:
1、激活系统组件,例如startActivity(Intent),startService(Intent)
2、在系统组件之间传递数据

Log

username:Lily gender:女 email:[email protected]

栗子

两个Activity之间数据传递,从MainActivity跳转到SecondActivity,同时传递数据

activity_main



    

    

    

    

        

        

    

    

    

MainActivity

public class MainActivity extends Activity implements View.OnClickListener {
    private EditText etUserName;
    private RadioButton rbGenderMale;
    private EditText etAge;
    private Button btnSubmit;

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

        etUserName = (EditText) findViewById(R.id.et_username);
        rbGenderMale = (RadioButton) findViewById(R.id.rb_male);
        etAge = (EditText) findViewById(R.id.et_age);
        btnSubmit = (Button) findViewById(R.id.btn_submit);
        btnSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String username = etUserName.getText().toString().trim();
        String gender = rbGenderMale.isChecked()?"男":"女";
          int age = Integer.parseInt(etAge.getText().toString());


        Intent intent = new Intent(this,SecondActivity.class);
        intent.putExtra("_username",username);
        intent.putExtra("_gender",gender);
        intent.putExtra("_age",age);
        startActivity(intent);
    }
}

SecondActivity

public class SecondActivity extends Activity {
    private Button btnStart;
    private Button btnStop;

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

        Intent intent = getIntent();

        String u = intent.getStringExtra("_username");
        String g = intent.getStringExtra("_gender");
        int a = intent.getIntExtra("_age",0);

        Log.d("Intent","姓名:"+u+" 性别:"+g+" 年龄:"+a);
    }
}

www.bkjia.comtruehttp://www.bkjia.com/qtjc/1304356.htmlTechArticleIntent的主要作用,Intent主要作用 Intent 意图 Intent在Android系统的作用: 1、激活系统组件,例如startActivity(Intent),startService(Intent) 2、在系统组...

相关文章

帮客评论

视觉看点