zoukankan      html  css  js  c++  java
  • Activity 切换(android)

    两个 activity 中相互切换,使用 Intent 对像,使用很简单

    Intent in = new Intent();
            in.setClass(this,active2.class);
            // 向 active2 传值
            in.putExtra("tt", et.getText().toString());
            this.startActivity(in);
            this.finish();

    详细代码

    main.xml 第一个 activity用的 xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width
    ="fill_parent"
        android:layout_height
    ="fill_parent"
        android:orientation
    ="vertical" >

        <TextView
            
    android:layout_width="fill_parent"
            android:layout_height
    ="wrap_content"
            android:text
    ="这是 active 1111 " />
        <EditText 
            
    android:id="@+id/et1"
            android:layout_width
    ="fill_parent"
            android:layout_height
    ="wrap_content"
            
    />
        <Button 
            
    android:id="@+id/btn1"
            android:layout_width
    ="wrap_content"
            android:layout_height
    ="wrap_content"
            android:text
    ="go to active2"
            
    />
    </LinearLayout>

    main2.xml 第二个 activity用的 xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width
    ="fill_parent"
        android:layout_height
    ="fill_parent"
        android:orientation
    ="vertical" >

        <TextView
            
    android:layout_width="fill_parent"
            android:layout_height
    ="wrap_content"
            android:text
    ="这是 active2 " />
         <EditText 
            
    android:id="@+id/et1"
            android:layout_width
    ="fill_parent"
            android:layout_height
    ="wrap_content"
            
    />
        <Button 
            
    android:id="@+id/btn1"
            android:layout_width
    ="wrap_content"
            android:layout_height
    ="wrap_content"
            android:text
    ="go to active1"
            
    />
    </LinearLayout>

    activetest java代码

    package zziss.android.activetest;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.Button;
    import android.widget.EditText;
    import android.view.View;
    public class ActiveTestActivity extends Activity implements View.OnClickListener {
        /** Called when the activity is first created. */
        private Button btn1;
        private EditText et;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            btn1 = (Button)this.findViewById(R.id.btn1);
            et   = (EditText)this.findViewById(R.id.et1);
            btn1.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent in = new Intent();
            in.setClass(this,active2.class);
            // 向 active2 传值
            in.putExtra("tt", et.getText().toString());
            this.startActivity(in);
            this.finish();
        }
    }

    active java 代码

    package zziss.android.activetest;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    public class active2 extends Activity  implements View.OnClickListener{
        private Button btn1;
        private EditText et;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main2);
            btn1 = (Button)this.findViewById(R.id.btn1);
            et   = (EditText)this.findViewById(R.id.et1);
            Bundle data = this.getIntent().getExtras();
            
            if (data.containsKey("tt"))
                et.setText(data.getString("tt"));
            
            btn1.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent in = new Intent();
            in.setClass(this,ActiveTestActivity.class);
            this.startActivity(in);
            this.finish();
        }
    }
  • 相关阅读:
    HDU 3681 Prison Break 越狱(状压DP,变形)
    POJ 2411 Mondriaan's Dream (状压DP,骨牌覆盖,经典)
    ZOJ 3471 Most Powerful (状压DP,经典)
    POJ 2288 Islands and Bridges (状压DP,变形)
    HDU 3001 Travelling (状压DP,3进制)
    POJ 3311 Hie with the Pie (状压DP)
    POJ 1185 炮兵阵地 (状压DP,轮廓线DP)
    FZU 2204 7
    POJ 3254 Corn Fields (状压DP,轮廓线DP)
    ZOJ 3494 BCD Code (数位DP,AC自动机)
  • 原文地址:https://www.cnblogs.com/zziss/p/2321762.html
Copyright © 2011-2022 走看看