android setcontentview到view绘制,Android使用setContentView实现页面的转换效果

一提到Android中页面的切换,你是不是只想到了startActivity启动另一个Activity?

其实在Android中,可以直接利用setContentView达到类似页面转换效果的!实现思路如下:

在第一个Activity的布局中添加一个Button,实现点击事件

点击该Button,调用setContentView,传入第二个页面的Layout,第二个页面就显示出来了

第二个页面的布局中仍然有一个Button,仍然实现其点击事件

点击该Button,调用setContentView,传入第一个页面的Layout,第一个页面就显示回来了

因此,有点类似相互嵌套调用,源代码如下:

public class ExampleActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main_page_layout);

Button button = findViewById(R.id.buttonGoToLayout2);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// 跳转到第二个页面

jumpToLayout2();

}

});

}

private void jumpToLayout2() {

// 设置第二个页面的布局

setContentView(R.layout.layout2);

Button button2 = findViewById(R.id.buttonGoToLayout1);

button2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// 在第二个页面中,点击Button,跳转到第一个页面

jumpToLayout1();

}

});

}

private void jumpToLayout1() {

// 设置第一个页面d的布局

setContentView(R.layout.main_page_layout);

Button button = findViewById(R.id.buttonGoToLayout2);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// 点击第一个页面的Button,跳转到第二个页面

jumpToLayout2();

}

});

}

}

两个布局文件如下:

1、第一个页面布局:main_page_layout.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center">

android:id="@+id/textView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="This is Layout One"

android:paddingTop="20dp"

android:textSize="30sp"/>

android:text="Go to Layout Two"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/buttonGoToLayout2"

android:layout_marginTop="20dp"

android:layout_below="@id/textView1"/>

2、第二个页面布局:layout2.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/black" >

android:id="@+id/textView2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="This is Layout Two"

android:paddingTop="20dp"

android:textColor="@android:color/white"

android:textSize="30sp"/>

android:text="Go to Layout One"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/buttonGoToLayout1"

android:layout_marginTop="20dp"

android:layout_below="@id/textView2"/>

通过setContentView实现页面切换,相比Activity切换有个特别的优点:

所有程序里的变量都存在相同的状态:类成员变量、类函数等,都可以在同一个Activity中直接获得,没有参数传递的问题。比如:

Layout1收集了用户输入的银行卡号码等付款信息,点击“下一步”进入Layout2显示订单信息,让用户确认,用户点击“确认”按钮后,进入Layout3进行付款的授权操作,整个过程没有变量的传递。

以上就是Android使用setContentView实现页面的转换效果的详细内容,更多关于Android 页面转换效果的资料请关注脚本之家其它相关文章!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值