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
原创粉丝点击