AIDE Android App基础教程_第二十二课答案-程序界面

第二十二课,本课中您将学习如何在程序中添加第二个用户界面 运行这个App。

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onComposeButtonClick(View view) 
    {
    	
	}
}

我们已经为界面添加了布局文件sub.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:gravity="left"
    android:padding="20dp"
    android:orientation="vertical" >
    
    <EditText
		android:inputType="textEmailSubject"
		android:hint="Subject"
		android:layout_width="match_parent"
		android:layout_height="wrap_content" />
	
	<EditText
		android:hint="Email"
		android:inputType="textEmailAddress"
		android:layout_width="match_parent"
		android:layout_height="wrap_content" />

</LinearLayout>

我们为sub.xml布局内容的界面新增Java类“SubActivity”

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;

public class SubActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set sub.xml as user interface layout
        setContentView(R.layout.sub);
    }
}

再在应用程序清单里新增无属性的“activity”元素

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aide.trainer.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk 
        android:minSdkVersion="11" 
        android:targetSdkVersion="17" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Holo.Light"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity>
        </activity>
    </application>

</manifest>

在“activity”元素里添加属性“android:label”属性并赋值为“Compose email”,它将会成为新增的activity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aide.trainer.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk 
        android:minSdkVersion="11" 
        android:targetSdkVersion="17" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Holo.Light"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
			android:label="Compose email">
		</activity>
    </application>

</manifest>

在“activity”元素里添加属性“android:theme”属性并赋值为“@android:style/Theme.Holo.Light”

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aide.trainer.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk 
        android:minSdkVersion="11" 
        android:targetSdkVersion="17" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Holo.Light"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
			android:label="Compose email"
			android:theme="@android:style/Theme.Holo.Light">
		</activity>
    </application>

</manifest>

在“activity”元素里添加属性“android:name”属性并赋值为“.SubActivity”,系统注册“SubActivity”Java类操作该activity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aide.trainer.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk 
        android:minSdkVersion="11" 
        android:targetSdkVersion="17" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Holo.Light"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
			android:name=".SubActivity"
			android:label="Compose email"
			android:theme="@android:style/Theme.Holo.Light">
		</activity>
    </application>

</manifest>

把“Intent”作为环境,“SubActivity.class”作为活动类,传入“this”构造函数,以此作为信息,在“onComposeButtonClick”方法内启动activity

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onComposeButtonClick(View view) 
    {
    	Intent intent = new Intent(this,SubActivity.class);
	}
}

现在调用“startActivity()”方法并传入参数“intent”

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onComposeButtonClick(View view) 
    {
    	Intent intent = new Intent(this,SubActivity.class);
    	startActivity(intent);
	}
}
 版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明
本文链接: https://www.aidemx.cn/2157.html

梦雪

专注于AIDE教程分享

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐

AIDE教程网
AIDE教程网