andorid_activity简单的入门

来源:互联网 发布:顶速网络机顶盒 编辑:程序博客网 时间:2024/05/21 19:18

Activity (组件)

一个界面对应一个activity

如何在应用程序里面创建多个界面

1. 声明一个布局2. 定义一个类, 继承activity    在activity里面的onCreate方法指定显示的布局  setContentView(R.layout.xxx);3. 登记注册activity

入门小示例
需求:从点击按钮从一个activity跳转到另外一个activity

第一步:布局
activity_main.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:onClick="showOther"        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="18dp"        android:text="点击跳转" /></RelativeLayout>

activity_other.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:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="这是第二个界面" /></RelativeLayout>
  1. 定义一个类, 继承activity
    在activity里面的onCreate方法指定显示的布局 setContentView(R.layout.xxx);

MainActivity的代码

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //设置了该界面,显示什么布局 (显示什么内容)        setContentView(R.layout.activity_main);    }    public void showOther(View v){    //这个startAvtivity(Intent intent)是跳转页面的核心方法。调用该方法,    //就会跳转到,意图对象,指定的类       //new Intent(this , OtherActivity.class)第一个参数是上下文,第二个是指定的类   //如果是跳转到Activity,那么这个类必须要继承Activity    startActivity(new Intent(this , OtherActivity.class));    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }}

OtherActivity的代码

public class OtherActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_other);    }}

AndroidManifest.xml清单文件

        //这也是注册activity,这个的是activity工程建立好就有的了         <activity                       android:name="com.itheima.mulitui.MainActivity"            android:label="@string/app_name" >            //这是意图过滤器,这有满足了意图过滤器activity,才能跳转过来,            //一般用于隐式 跳转            <intent-filter>               // 动作 : 这里声明了应用程序的入口界面                <action android:name="android.intent.action.MAIN" />                 //分类: 指定了再应用程序列表里面有一个图标                 <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        //我们自己另外添加的otherActivity,登记注册Activity         <activity            android:name="com.itheima.mulitui.OtherActivity"            android:label="02多界面应用"                       >        </activity>

意图过滤器的小说明

 <intent-filter>                动作 : 这里声明了应用程序的入口界面                <action android:name="android.intent.action.MAIN" />                分类: 指定了再应用程序列表里面有一个图标                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>
0 0
原创粉丝点击