android開發學習筆記(三)Activity跳轉以及傳值

一:步驟:android

1建立Intent對象,參數中指定將要跳轉到的classapp

2經過Intent對象的putExtra()方法指定要傳到下一個頁面的數據。方法參數用鍵值對。ide

3經過調用startActivity(Intent intent)方法或者startActivityForResult(Intent intent,int requestCode)方法啓動跳轉。this

startActivtyForResult():Activity A跳轉到Activity B,Activity B能夠返回數據給Activity A。spa

startActivity():Activity A跳轉到 Activity B並能夠傳值。code

4跳轉到另外一個Activity後,能夠經過getIntent().getExtras()方法得到Bundle對象,經過Bundle對象的getString()/getInt()...方法獲取到值。xml

5若是是經過startActivityForResult()跳轉的Activity,那麼結束了跳轉的Activity後還要在被跳轉的Activity類中實現onActivityResult()方法.對象

二:實例blog

package com.example.apploading;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Lauching extends Activity {
    private EditText Username=null;
    private Button  btn=null;

    private OnClickListener mListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i=new Intent(Lauching.this,Exit.class);
            String s=Username.getText().toString();
            i.putExtra("name", s);
            //startActivityForResult(i,R.layout.activity_exit);
            startActivity(i);
        }

    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        Username=(EditText)findViewById(R.id.InputUserName);
        btn=(Button)findViewById(R.id.Lauching);
        btn.setOnClickListener(mListener);
    }
}
package com.example.apploading;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Exit extends Activity implements OnClickListener{
    Button ExitBtn;
    Button ReturnBtn;
    TextView Name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exit);
        ExitBtn=(Button)findViewById(R.id.ExitBtn);
        ReturnBtn=(Button)findViewById(R.id.returnBtn);
        Name=(TextView)findViewById(R.id.Name);
        String name=getIntent().getExtras().getString("name");
        Name.setText(name);
        ExitBtn.setOnClickListener(this);
        ReturnBtn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.exit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.ExitBtn:
            finish();
            break;
        case R.id.returnBtn:
            Intent i=new Intent(this,Lauching.class);
            startActivity(i);
        }
    }   
}
相關文章
相關標籤/搜索