赞
踩
1.定义一个类,继承Activity:在该类中重写 Activity的 onCreate()方法
- package demo;
-
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.view.ViewGroup;
-
-
- import com.cszs.jgdnc.mi.R;
-
-
- import demo.SecretUrlView;
-
-
- public class SecretUrlActivity extends Activity {
-
- public ViewGroup urlView_container;
- private SecretUrlView urlView = null;
-
-
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.url_layout) ;
- InitUrlSecretView();
- }
-
-
- public void InitUrlSecretView(){
- if(urlView!=null){
- urlView.showUrlView();
- return;
- }
- this.urlView_container = findViewById(R.id.view_url_container);
- urlView = new SecretUrlView(this);
- }
-
-
- public void toMainActivity(){
- Intent intent = new Intent(SecretUrlActivity.this,MainActivity.class) ;
- startActivity(intent) ;
- }
- }
重写方法,选中该java文件,然后选中菜单栏中Code:
2.在layout文件夹中,为该Activity添加布局文件:文件名必须是小写
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- xmlns:app="http://schemas.android.com/apk/res-auto">
-
-
-
-
- <FrameLayout
- android:id="@+id/view_url_container"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginLeft="0dp"
- android:layout_marginTop="0dp"
- android:layout_marginRight="0dp"
- android:layout_marginBottom="0dp"
- android:background="#FFFFFF">
-
-
-
-
- <WebView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/wv"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_alignParentTop="true" />
-
-
-
-
- <ImageView
- android:id="@+id/view_url_close"
- android:layout_width="32dp"
- android:layout_height="32dp"
- android:layout_marginStart="9dp"
- android:layout_marginTop="20dp"
- android:layout_marginEnd="400dp"
- android:contentDescription="关闭按钮"
- android:src="@drawable/float_hide_tip_sel"
- app:layout_constraintBottom_toTopOf="@+id/view_feedBox_image"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.78"
- app:layout_constraintStart_toEndOf="@+id/view_feedBox_image"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.0" />
-
-
-
-
- </FrameLayout>
-
-
- </RelativeLayout>
3.在 新创建的 Activity.java 中设置该Activity所使用的 布局文件。即在 onCreate 方法中添加如下代码:
setContentView(R.layout.url_layout) ;
4.在AndroidManifest.xml中注册该 Activity:想要启动的Activity一定要先在 Manifest文件中注册
- <activity
-
- android:name="demo.SecretUrlActivity"
-
- android:label="SecondActivity" >
-
- </activity>
注意:想要哪个Activity作为程序的默认启动的Activity的时候,需要在注册的时候添加下面的代码:
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
5.在MainActivity中,注册点击事件,当点击某个按钮的时候,打开新的Activity:
这种方法会重新初始化这个Activity
- url_href.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.e("showPrivacy", "点击跳转url界面");
- Intent intent = new Intent(MainActivity.this,SecretUrlActivity.class) ;
- startActivity(intent);
- }
- });
6.当要跳转回去 MainActivity的时候,也是同样的代码:写在SecretUrlActivity中
这样是有问题的,会重新初始化MainActivity,所以正确的做法是,直接销毁这个SecretUrlActivity ,调用 finish()
- public void toMainActivity(){
- Intent intent = new Intent(SecretUrlActivity.this,MainActivity.class) ;
- startActivity(intent) ;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。