關於在Android中Activity頁面跳轉的方法

一.無返回結果的頁面跳轉java

1.建立兩個類FActivity.java和SActivity.javaandroid

2.建立兩個layout目錄下的factivity.xml和sactivity.xmlide

3.在AndroidManifest配置文件裏註冊FActivity.java和SActivity.java,並把原來MainActivity註冊信息中的this

<intent-filter>
  <action android:name="android.intent.action.MAIN" />

  <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

刪去,這幾句是設置初始啓動的Activity頁面spa

4.在FActivity的註冊信息中添加code

<intent-filter>
  <action android:name="android.intent.action.MAIN" />

  <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

5.實現從FActivity頁面中的button1點擊後跳轉到SActivity頁面xml

 

FActivity.javablog

public class FActivity extends Activity{

  private Button btn1;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.factivity);

    btn1 = (Button) findViewById(R.id.button1);

    btn1.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
      // TODO Auto-generated method stub
      Intent intent = new Intent(FActivity.this,SActivity.class);
      startActivity(intent);
    }

   });
  }
}

 

SActivity.javaget

public class SActivity extends Activity{

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sactivity);
  }

}

 

factivity.xmlit

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一個頁面" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳轉第二個頁面" />

 

sactivity.xml

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是第二個頁面" />

 

 

二.有返回結果的頁面跳轉

1.建立兩個類FActivity.java和SActivity.java

2.建立兩個layout目錄下的factivity.xml和sactivity.xml

3.在AndroidManifest配置文件裏註冊FActivity.java和SActivity.java,並把原來MainActivity註冊信息中的

<intent-filter>
  <action android:name="android.intent.action.MAIN" />

  <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

刪去,這幾句是設置初始啓動的Activity頁面

4.在FActivity的註冊信息中添加

<intent-filter>
  <action android:name="android.intent.action.MAIN" />

  <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

5.實現從FActivity頁面中的button點擊後跳轉到SActivity頁面,再點擊SActivity的button從SActivity傳回一個值到FActivity

 

FActivity.java

public class FActivity extends Activity{

  private Button btn2;
  private TextView tv2;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.factivity);

    btn2 = (Button) findViewById(R.id.button2);
    tv2 = (TextView) findViewById(R.id.textView2);


    btn2.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
      // TODO Auto-generated method stub
      Intent intent = new Intent(FActivity.this,SActivity.class);
      startActivityForResult(intent, 1);
    }
  });
 }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==1&&resultCode==2)
    {
      String content=data.getStringExtra("data");
      tv2.setText(content);
    }
  }
}

 

SActivity

public class SActivity extends Activity{

  private Button btn1;
  private String content="我是傳回來的值";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sactivity);

    btn1 = (Button) findViewById(R.id.button1);

    btn1.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
      // TODO Auto-generated method stub
      Intent data = new Intent();
      data.putExtra("data", content);
      setResult(2,data);
      finish();
    }
  });
 }

}

 

factivity.xml

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="傳值的的跳轉第二個界面" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="結果" />

 

sactivity.xml

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帶值返回" />
相關文章
相關標籤/搜索