• Android——Intent,Bundle


    • Intent——切换activity
      intent.setClass(first.this,second.class);
      startActivity(intent);
    • Bundle——切换时数据传递
      //跳转
      Intent intent = new Intent();
      //跳转时数据传输
      Bundle bundle = new Bundle();
      intent.setClass(MainActivity.this, SecondActivity.class);
      bundle.putString("message", str);
      intent.putExtras(bundle);
      startActivity(intent);
      Bundle bundle = this.getIntent().getExtras();
      String message = bundle.getString("message");

    Source Code:

    package com.example.intent;
    
    import android.os.Bundle;
    import android.R.string;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends Activity {
        
        private EditText myText;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            Button button = (Button)findViewById(R.id.button1);
            myText = (EditText)findViewById(R.id.editText1);
            
            button.setOnClickListener(new OnClickListener() {
                @Override 
                public void onClick(View v) {
                    
                    String str = myText.getText().toString();
                    //跳转
                    Intent intent = new Intent();
                    //跳转时数据传输
                    Bundle bundle = new Bundle();
                    
                    intent.setClass(MainActivity.this, SecondActivity.class);
                    bundle.putString("message", str);
                    intent.putExtras(bundle);
                    
                    
                    startActivity(intent);
                    
                }
            });
            
            
        }
    
        
    }
    MainActiviy.java
    package com.example.intent;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.TextureView;
    import android.widget.TextView;
    
    public class SecondActivity extends Activity{
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);
            
            Bundle bundle = this.getIntent().getExtras();
            String message = bundle.getString("message");
            
            TextView myText = (TextView)findViewById(R.id.textView2);
            myText.setText(message);
            
        }
        
    }
    SecondAcitivity.java
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">Intent</string>
        <string name="action_settings">Settings</string>
        <string name="hello_world">Hello world!</string>
        <string name="Tree">TreeDream</string>
    
    </resources>
    strings.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Tree" />
        
        <TextView
               android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/x" />
    
    
    
    </LinearLayout>
    second.xml
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="51dp"
            android:text="Turn to the secondActivity." />
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:ems="10" >
    
            <requestFocus />
        </EditText>
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/textView1"
            android:src="@drawable/y" />
    
    </RelativeLayout>
    activity_main.xml

  • 相关阅读:
    Bootstrap3基础 thumbnail 圆角类型的div块
    Bootstrap3基础 text-muted/success... 辅助类样式 情景文本颜色
    Bootstrap3基础 text-right/left/center 设置标题右对齐、左对齐、居中
    Bootstrap3基础 table-striped 表格实现隔行换色(浅灰色与白色交替)
    Bootstrap3基础 table-condensed 表格中的单元格紧凑一些
    Bootstrap3基础 table-responsive 响应式表格
    Bootstrap3基础 table-bordered/hover 表格加外边框和鼠标悬停对应行的背景色加深
    Bootstrap3基础 page-header 标题下加分割线
    iOS Swift编程语言
    【强烈推荐】XCODE的插件之王
  • 原文地址:https://www.cnblogs.com/TreeDream/p/7932307.html
Copyright © 2020-2023  润新知