我是Android编程的新手,我正在尝试为我的应用创建一个新的主要活动(取自Android教程网站)。我原来的主要活动称为“MainActivity”。我想成为我的主要活动的新活动被称为“主页”,它应该包含一个按钮,点击后会生成“MainActivity”。我不确定我应该在清单中包含有关新页面“主页”,homepage.xml和按钮的信息。特定代码将不胜感激。
首页:
package com.myphoneapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HomePage extends Activity {
private Button ScheduleBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
ScheduleBtn = (Button) findViewById(R.id.home_btn);
ScheduleBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(HomePage.this, MainActivity.class);
HomePage.this.startActivity(myIntent);
}
});
}
}
homepage.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" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to ClearLight"
android:id="@+id/home_btn"
/>
</LinearLayout>
MainActivity:
package com.myphoneapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myphoneapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.myphoneapp.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.myphoneapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myphoneapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myphoneapp.MainActivity" />
</activity>
<activity
android:name="com.myphoneapp.HomePage"
android:label="@string/homepage" android name="MainActivity"
android:parentActivityName="com.example.myphoneapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myphoneapp.MainActivity" />
</activity>
</application>
</manifest>
答案 0 :(得分:1)
要使HomePage
成为您的第一个活动,请编辑您的清单文件,使其具有action.MAIN
的意图过滤器。而且您不必在Manifest文件中定义任何有关布局的内容。只有活动声明(你已经拥有)
所以你的新清单文件看起来像
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.myphoneapp.MainActivity"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name="com.myphoneapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myphoneapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myphoneapp.MainActivity" />
</activity>
<activity
android:name="com.myphoneapp.HomePage"
android:label="@string/homepage"
android:parentActivityName="com.example.myphoneapp.MainActivity" >
<!-- Move the intent filter to HomePage -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myphoneapp.MainActivity" />
</activity>
</application>
</manifest>
对于启动mainActivity的按钮,您已经在HomePage.java
ScheduleBtn = (Button) findViewById(R.id.home_btn);
ScheduleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(HomePage.this, MainActivity.class);
HomePage.this.startActivity(myIntent);
}
});
此代码(取自您的HomePage.java
从意图
答案 1 :(得分:0)
像这样更改您的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myphoneapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.myphoneapp.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomePage"
</activity>
</application>
答案 2 :(得分:0)
在清单文件中,您的主页活动必须是您的主要活动,因此您的清单应如下所示:
<activity
android:name="com.myphoneapp.MainActivity"
android:label="@string/homepage" android name="MainActivity"
android:parentActivityName="com.example.myphoneapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myphoneapp.MainActivity" />
</activity>
<activity
android:name="com.myphoneapp.HomePage"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>