个人4/26博客

Android页面跳转

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center">

<Button
android:id="@+id/bt_intentmain"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="跳过去"
/>
<Button
android:id="@+id/bt_intentOne"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="按钮一"
/>
<Button
android:id="@+id/bt_intentTwo"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="按钮二"
/>
<Button
android:id="@+id/bt_intentThree"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="按钮三"
/>

</LinearLayout>

代码:

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button mBtIntent,mBtIntentOne,mBtIntentTwo,mBtIntentThree;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("TAG","onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mBtIntent.setOnClickListener(this);
mBtIntentOne.setOnClickListener(this);
mBtIntentTwo.setOnClickListener(this);
mBtIntentThree.setOnClickListener(this);
setTitle("页面A");


}


private void initView(){
mBtIntent = findViewById(R.id.bt_intentmain);
mBtIntentOne = findViewById(R.id.bt_intentOne);
mBtIntentTwo = findViewById(R.id.bt_intentTwo);
mBtIntentThree = findViewById(R.id.bt_intentThree);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_intentmain:
IntentOne();
break;
case R.id.bt_intentOne:
IntentTwo();
break;
case R.id.bt_intentTwo:
IntentThree();
break;
case R.id.bt_intentThree:
IntentFour();
break;
default:
break;
}
}

 

private void IntentOne(){
Intent intent = new Intent(MainActivity.this,HomeActivity.class);
startActivity(intent);
}

private void IntentTwo(){
Intent intent = new Intent();
intent.setClass(MainActivity.this,HomeActivity.class);
startActivity(intent);
}

private void IntentThree(){
Intent intent = new Intent();
ComponentName componentName = new ComponentName(MainActivity.this,HomeActivity.class);
intent.setComponent(componentName);
startActivity(intent);

}

private void IntentFour(){
startActivity(new Intent(MainActivity.this,HomeActivity.class));
}
}

posted @   liu_ru_jun  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示