安卓 |创建多屏应用
本文展示了如何创建一个 android 应用程序以从一个活动移动到另一个活动。
以下是创建一个简单的 Android 应用程序以从一个活动移动到另一个活动的步骤。
STEP-1:创建新项目,您的项目屏幕如下所示。
第 2 步:您将拥有 xml 和活动Java文件,下面给出了这两个文件的路径。
第 3 步:打开您的 xml 文件并添加按钮,因为单击此按钮后,我们将移至第二个活动,如下所示。为消息添加 TextView。将 ID 分配给 Button 和 TextView 。
STEP-4:现在我们必须创建另一个活动(SecondActivity)以从一个活动移动到另一个活动。创建第二个活动并转到 android 项目 > 文件 > 新建 > 活动 > 空活动
STEP-5:现在打开你的第二个xml文件,这个文件的路径和第一个xml文件一样。为消息添加 TextView 并添加 2 个按钮,一个用于下一个活动,第二个用于上一个活动。将 ID 分配给 Textview 和两个 Button。第二个活动如下图所示:
STEP-6:现在,我们必须创建与第二个活动相同的第三个活动,并且该文件的路径也与另一个相同。(“现在,您可以创建许多这样的活动”)这里我们为消息添加TextView,为消息添加一个按钮转到上一个活动。如下图所示
第 7 步:现在,打开您的第一个活动Java文件。定义 Button(next_button 或可以是 previous_button)和 TextView 变量,使用 findViewById() 获取 Button 和 TextView。
STEP-8:我们需要将点击监听器添加到所有按钮(next_button或者可以是previous_button)。
第 9 步:当在 onclicklistener 方法中单击按钮时,创建一个 Intent 以启动一个称为另一个活动的活动。
第10 步:对每个活动重复第 7、8、9 步。
第 11 步:现在运行应用程序并单击可以转到第二个活动的按钮。
在第一个活动中,这里只有一个 Button 和 TextView
OneActivity的完整代码。下面是 Firstactivity 的Java或 activity_oneactivity.xml。
文件名:activity_oneactivity.xml
XML
Java
// Each new activity has its own layout and Java files,
package org.geeksforgeeks.navedmalik.multiplescreenapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Oneactivity extends AppCompatActivity {
// define the global variable
TextView question1;
// Add button Move to Activity
Button next_Activity_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oneactivity);
// by ID we can use each component which id is assign in xml file
// use findViewById() to get the Button
next_Activity_button = (Button)findViewById(R.id.first_activity_button);
question1 = (TextView)findViewById(R.id.question1_id);
// In question1 get the TextView use by findViewById()
// In TextView set question Answer for message
question1.setText("Q 1 - How to pass the data between activities in Android?\n"
+ "\n"
+ "Ans- Intent");
// Add_button add clicklistener
next_Activity_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Intents are objects of the android.content.Intent type. Your code can send them
// to the Android system defining the components you are targeting.
// Intent to start an activity called SecondActivity with the following code:
Intent intent = new Intent(Oneactivity.this, SecondActivity.class);
// start the activity connect to the specified class
startActivity(intent);
}
});
}
}
XML
Java
package org.geeksforgeeks.navedmalik.multiplescreenapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
// define the global variable
TextView question2;
// Add button Move to next Activity and previous Activity
Button next_button, previous_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// by ID we can use each component which id is assign in xml file
// use findViewById() to get the both Button and textview
next_button = (Button)findViewById(R.id.second_activity_next_button);
previous_button = (Button)findViewById(R.id.second_activity_previous_button);
question2 = (TextView)findViewById(R.id.question2_id);
// In question1 get the TextView use by findViewById()
// In TextView set question Answer for message
question2.setText("Q 2 - What is ADB in android?\n"
+ "\n"
+ "Ans- Android Debug Bridge");
// Add_button add clicklistener
next_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Intents are objects of the android.content.Intent type. Your code can send them
// to the Android system defining the components you are targeting.
// Intent to start an activity called ThirdActivity with the following code:
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
// start the activity connect to the specified class
startActivity(intent);
}
});
// Add_button add clicklistener
previous_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Intents are objects of the android.content.Intent type. Your code can send them
// to the Android system defining the components you are targeting.
// Intent to start an activity called oneActivity with the following code:
Intent intent = new Intent(SecondActivity.this, Oneactivity.class);
// start the activity connect to the specified class
startActivity(intent);
}
});
}
}
XML
Java
package org.geeksforgeeks.navedmalik.multiplescreenapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.widget.TextView;
public class ThirdActivity extends AppCompatActivity {
// define the global variable
TextView question3;
// Add button Move previous activity
Button previous_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
// by ID we can use each component which id is assign in xml file
// use findViewById() to get the Button and textview.
previous_button = (Button)findViewById(R.id.third_activity_previous_button);
question3 = (TextView)findViewById(R.id.question3_id);
// In question1 get the TextView use by findViewById()
// In TextView set question Answer for message
question3.setText("Q 3 - How to store heavy structured data in android?\n"
+ "\n"
+ "Ans- SQlite database");
// Add_button add clicklistener
previous_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Intents are objects of the android.content.Intent type. Your code can send them
// to the Android system defining the components you are targeting.
// Intent to start an activity called SecondActivity with the following code:
Intent intent = new Intent(ThirdActivity.this, SecondActivity.class);
// start the activity connect to the specified class
startActivity(intent);
}
});
}
}
文件名:OneActivity。Java
Java
// Each new activity has its own layout and Java files,
package org.geeksforgeeks.navedmalik.multiplescreenapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Oneactivity extends AppCompatActivity {
// define the global variable
TextView question1;
// Add button Move to Activity
Button next_Activity_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oneactivity);
// by ID we can use each component which id is assign in xml file
// use findViewById() to get the Button
next_Activity_button = (Button)findViewById(R.id.first_activity_button);
question1 = (TextView)findViewById(R.id.question1_id);
// In question1 get the TextView use by findViewById()
// In TextView set question Answer for message
question1.setText("Q 1 - How to pass the data between activities in Android?\n"
+ "\n"
+ "Ans- Intent");
// Add_button add clicklistener
next_Activity_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Intents are objects of the android.content.Intent type. Your code can send them
// to the Android system defining the components you are targeting.
// Intent to start an activity called SecondActivity with the following code:
Intent intent = new Intent(Oneactivity.this, SecondActivity.class);
// start the activity connect to the specified class
startActivity(intent);
}
});
}
}
注意:这里我们将为消息添加 Next Button 和 previous Button 和 textView。
SecondActivity的完整代码。下面是第二个活动的Java或 activity_second.xml。
文件名:activity_second.xml
XML
文件名:第二活动。Java
Java
package org.geeksforgeeks.navedmalik.multiplescreenapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
// define the global variable
TextView question2;
// Add button Move to next Activity and previous Activity
Button next_button, previous_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// by ID we can use each component which id is assign in xml file
// use findViewById() to get the both Button and textview
next_button = (Button)findViewById(R.id.second_activity_next_button);
previous_button = (Button)findViewById(R.id.second_activity_previous_button);
question2 = (TextView)findViewById(R.id.question2_id);
// In question1 get the TextView use by findViewById()
// In TextView set question Answer for message
question2.setText("Q 2 - What is ADB in android?\n"
+ "\n"
+ "Ans- Android Debug Bridge");
// Add_button add clicklistener
next_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Intents are objects of the android.content.Intent type. Your code can send them
// to the Android system defining the components you are targeting.
// Intent to start an activity called ThirdActivity with the following code:
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
// start the activity connect to the specified class
startActivity(intent);
}
});
// Add_button add clicklistener
previous_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Intents are objects of the android.content.Intent type. Your code can send them
// to the Android system defining the components you are targeting.
// Intent to start an activity called oneActivity with the following code:
Intent intent = new Intent(SecondActivity.this, Oneactivity.class);
// start the activity connect to the specified class
startActivity(intent);
}
});
}
}
注意:这里我们只为消息添加 Next Button 和 textView。
ThirdActivity 的完整代码。下面是第三个活动的Java或 activity_third.xml。
文件名:activity_third.xml
XML
文件名:ThirdActivity。Java
Java
package org.geeksforgeeks.navedmalik.multiplescreenapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.widget.TextView;
public class ThirdActivity extends AppCompatActivity {
// define the global variable
TextView question3;
// Add button Move previous activity
Button previous_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
// by ID we can use each component which id is assign in xml file
// use findViewById() to get the Button and textview.
previous_button = (Button)findViewById(R.id.third_activity_previous_button);
question3 = (TextView)findViewById(R.id.question3_id);
// In question1 get the TextView use by findViewById()
// In TextView set question Answer for message
question3.setText("Q 3 - How to store heavy structured data in android?\n"
+ "\n"
+ "Ans- SQlite database");
// Add_button add clicklistener
previous_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Intents are objects of the android.content.Intent type. Your code can send them
// to the Android system defining the components you are targeting.
// Intent to start an activity called SecondActivity with the following code:
Intent intent = new Intent(ThirdActivity.this, SecondActivity.class);
// start the activity connect to the specified class
startActivity(intent);
}
});
}
}
输出: