Android开发怎么从一个activity(通过button)打开另一个activity
2个回答
展开全部
1. 在rec/layout/main.xml中加入一个Btton:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="my button"
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
2 .在src/your_project.java中代码:
package your_project;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
public class your_project extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.my_button); //找到在main.xml中创建的Button
button1.setOnClickListener(button_listener); //给Buton设置一个listener,这里注意的是setOnClickListener()的参数如果是this,就要在 “public class your_project extends Activity“的后面加上” implements OnClickListener“ ,否则会出错;这里是自己建立的listener所以不用
}
private Button.OnClickListener button_listener = new Button.OnClickListener(){ //创建button listener
public void onClick(View v){
Intent intent = new Intent();
intent.setClass(your_project.this,TestActivity.class);// TestActivity 是要跳转到的Activity,需要在src下手动建立TestActivity.java文件
startActivity(intent);
setTitle("other Activity!");
}
};
}
3. 在AndroidManifest.xml文件中注册你要跳转的Activity:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your_project"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="your_project"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="TestActivity"> </activity>
</application>
<uses-sdk android:minSdkVersion="2" />
</manifest>
4 . 在src下创建TestActivity.java文件:
package your_project;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("other activity");
setContentView(R.layout.test_activity); // 此Activity 与test_activity.xml 相关联。test_activity.xml 在res/layout中手动建立。
}
}
5 在res/layout中建立test_activity.xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/Text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is another activity"
/>
</LinearLayout>
这样一个最基本的通过一个BUTTON 转到另一个ACTIVITY就完成了。保存运行。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="my button"
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
2 .在src/your_project.java中代码:
package your_project;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
public class your_project extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.my_button); //找到在main.xml中创建的Button
button1.setOnClickListener(button_listener); //给Buton设置一个listener,这里注意的是setOnClickListener()的参数如果是this,就要在 “public class your_project extends Activity“的后面加上” implements OnClickListener“ ,否则会出错;这里是自己建立的listener所以不用
}
private Button.OnClickListener button_listener = new Button.OnClickListener(){ //创建button listener
public void onClick(View v){
Intent intent = new Intent();
intent.setClass(your_project.this,TestActivity.class);// TestActivity 是要跳转到的Activity,需要在src下手动建立TestActivity.java文件
startActivity(intent);
setTitle("other Activity!");
}
};
}
3. 在AndroidManifest.xml文件中注册你要跳转的Activity:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your_project"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="your_project"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="TestActivity"> </activity>
</application>
<uses-sdk android:minSdkVersion="2" />
</manifest>
4 . 在src下创建TestActivity.java文件:
package your_project;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("other activity");
setContentView(R.layout.test_activity); // 此Activity 与test_activity.xml 相关联。test_activity.xml 在res/layout中手动建立。
}
}
5 在res/layout中建立test_activity.xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/Text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is another activity"
/>
</LinearLayout>
这样一个最基本的通过一个BUTTON 转到另一个ACTIVITY就完成了。保存运行。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询