andriod学习——启动第二个activity
来源:互联网 发布:检测 android ios js 编辑:程序博客网 时间:2024/06/05 19:40
1、 一个十分简单的android app,第一个活动页面为一输入框与一个发送按钮 ,输入信息点击发送按钮将打开第二个界面,且显示刚才输入的内容。(工具:android studio 2.3 )
2、效果图(为了截屏用了虚拟机发现真是比真机卡太多了):
hhhh~是不是超级简单呢。
3、activity与 layout全部文件如下图:
4、代码
(1)、layout1文件
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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="com.example.xx.http_app.activity1"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" tools:layout_editor_absoluteY="-2dp"> <EditText android:id="@+id/msg" android:layout_width="400px" android:layout_height="wrap_content" /> <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送" /> </LinearLayout></android.support.constraint.ConstraintLayout>(2)、layout2文件
<?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"> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="TextView" /></LinearLayout>
(3)、activity1文件
捕获EditText输入的内容并赋值给公共常量;
创建第二个activity:Intent intent = new Intent(当前activity.this,要启动的另一个activity.class);
starActivity(intent)启动第二个activity;
Intent intent = new Intent(activity1.this,activity2.class);
public class activity1 extends Activity { public final static String EXTRA_MESSAGE = "com.example.bao.http_app.MESSAGE";//定义一个公共常量 private Button bt; EditText msg=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_1); bt=(Button)findViewById(R.id.bt); msg=(EditText)findViewById(R.id.msg); msg.setText("这是第一个"); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(activity1.this,activity2.class); EditText editText = (EditText) findViewById(R.id.msg); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); } }); }}(4)、activity2文件
public class activity2 extends Activity { private TextView tv2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_2); tv2=(TextView)findViewById(R.id.tv2); Intent intent = getIntent(); String message = intent.getStringExtra(activity1.EXTRA_MESSAGE); // Get the message from the intent tv2.setText(message); }}
额。记得在AndroidManifest.xml中添加activity2
。
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.bao.http_app"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".activity1"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".activity2" /> <!-- 加入第二个activity --> </application> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /></manifest>
以上就是这个程序全部的代码了
end
0 0
- andriod学习——启动第二个activity
- Android学习笔记——Activity之显式启动第二个Activity
- 启动第二个Activity
- 由一个activity启动第二个activity
- Andriod开发——Activity数据传递的学习
- 创建第二个activity并启动
- Andriod学习笔记 Activity
- Andriod 学习——Activity之间的跳转,以及Activity的生命周期。
- andriod学习笔记——andriod框架
- Android编程权威指南(第二版)学习笔记(五)—— 第5章 第二个 Activity
- andriod Activity 的四种启动方式
- Andriod 之 Activity的四大启动模式。
- andriod 多个Activity之间共享数据
- 学习笔记—Activity的启动模式
- 创建第二个activity
- android学习——第二个程序
- 第二十一天 Activity的生命周期、显式启动第二个界面、四大布局(layout)
- Android学习第二天——Intent & Activity的生命周期
- 各大网站JQuery的CDN库
- js中的apply方法和call方法区别
- goChat项目总结
- hibernate service
- 北航计算机机试16字符串的解析
- andriod学习——启动第二个activity
- CodeForces 143DHelp General
- UVA
- 笔记11
- MySQL主从同步初探
- POJ2761-Treap
- jQuery中的text()、html()和val()
- ajax 异步的javascript和xml
- Linux介绍