在Android应用程序中,当单击另一个活动中的按钮时,如何启动一个新活动(GUI),以及如何在这两个活动之间传递数据?
当前回答
点击按钮时:
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent= new Intent(getApplicationContext(), NextActivity.class);
intent.putExtra("data", value); //pass data
startActivity(intent);
}
});
从nex战术vity.class接收额外的数据:
Bundle extra = getIntent().getExtras();
if (extra != null){
String str = (String) extra.get("data"); // get a object
}
其他回答
从这个活动启动另一个活动,你也可以通过Bundle对象传递参数。
Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("USER_NAME", "xyz@gmail.com");
startActivity(intent);
在另一个活动(YourActivity)中检索数据
String s = getIntent().getStringExtra("USER_NAME");
以马内利,
我认为额外的信息应该放在启动活动之前,否则数据将不可用,如果你在NextActivity的onCreate方法中访问它。
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("key", value);
CurrentActivity.this.startActivity(myIntent);
科特林
第一个活动
startActivity(Intent(this, SecondActivity::class.java)
.putExtra("key", "value"))
第二个活动
val value = getIntent().getStringExtra("key")
建议
总是把钥匙放在固定的文件,以更有管理的方式。
companion object {
val PUT_EXTRA_USER = "user"
}
startActivity(Intent(this, SecondActivity::class.java)
.putExtra(PUT_EXTRA_USER, "value"))
当用户点击按钮时,直接在XML中,就像这样:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextButton"
android:onClick="buttonClickFunction"/>
使用属性android:onClick我们声明方法名必须出现在父活动上。所以我必须在我们的活动中创建这个方法,像这样:
public void buttonClickFunction(View v)
{
Intent intent = new Intent(getApplicationContext(), Your_Next_Activity.class);
startActivity(intent);
}
目前的回答很好,但初学者需要一个更全面的回答。在Android中有3种不同的方式来启动一个新活动,它们都使用Intent类;意图| Android开发者。
使用按钮的onClick属性。(初学者) 通过匿名类分配OnClickListener()。(中间) 活动范围接口方法使用switch语句。(不是“Pro”)
如果你想继续,这里是我示例的链接:
使用按钮的onClick属性。(初学者)
按钮有一个onClick属性,可以在.xml文件中找到:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToAnActivity"
android:text="to an activity" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToAnotherActivity"
android:text="to another activity" />
在Java类中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
public void goToAnActivity(View view) {
Intent intent = new Intent(this, AnActivity.class);
startActivity(intent);
}
public void goToAnotherActivity(View view) {
Intent intent = new Intent(this, AnotherActivity.class);
startActivity(intent);
}
优点:易于动态制作,模块化,并且可以轻松地将多个onclick设置为相同的意图。
缺点:回顾时可读性较差。
通过匿名类分配OnClickListener()。(中间)
这是当你为每个按钮设置一个单独的setOnClickListener()并用自己的意图覆盖每个onClick()时。
在Java类中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), AnActivity.class);
view.getContext().startActivity(intent);}
});
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), AnotherActivity.class);
view.getContext().startActivity(intent);}
});
优点:容易在飞行中制作。
缺点:会有很多匿名类,这将使可读性在审查时变得困难。
活动范围接口方法使用switch语句。(不是“Pro”)
这是当您在onClick()方法中为按钮使用switch语句来管理所有Activity的按钮时。
在Java类中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
Intent intent1 = new Intent(this, AnActivity.class);
startActivity(intent1);
break;
case R.id.button2:
Intent intent2 = new Intent(this, AnotherActivity.class);
startActivity(intent2);
break;
default:
break;
}
优点:易于按钮管理,因为所有按钮意图都注册在一个onClick()方法中
关于问题的第二部分,传递数据,请参阅如何在Android应用程序的活动之间传递数据?
编辑:不——“专业”
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件