Сильно не расслабляясь решил показать как вызывать другие Activity, например при нажатии кнопки.
Что будем делать?
Довольно таки часто в приложениях Android замечается переход между Activity (view), как это сделать мы и рассмотрим сейчас.
Шаг 1
Добавим в main.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" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go to Activity" android:id="@+id/btnActTwo" android:onClick="goNewView"/> </LinearLayout>
Шаг 2
На кнопке уже висит обработчик android:onClick=”goNewView” давайте его напишем:
public void goNewView(View v){ switch (v.getId()) { case R.id.btnActTwo: Intent intent = new Intent(this, NewActivity.class); startActivity(intent); break; default: break; } }
Шаг 3
Создаем новый Activity:
package com.devcolibri.GoActivity; import android.app.Activity; import android.os.Bundle; public class NewActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.two); } }
В строке 10-й используется метод который и присваивает нашему Activity view -> setContentView(R.layout.two);
Шаг 4
Создаем View назовем её two.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" This is Activity Two" /> </LinearLayout>
Шаг 5
И не забываем добавить в AndroidManifest.xml наше activity:
<activity android:name="com.devcolibri.NewActivity" />
Без этого у вас ничего не будет работать. Данная строка говорит, что в ходе выполнения приложения будет зайдействован NewActivity.
Шаг 6
Запускаем!
ПОХОЖИЕ ПУБЛИКАЦИИ
- None Found
5 комментариев к статье "Android. Как вызвать Activity"