简体   繁体   English

如何使用Android注释通过Intent获得额外的数据?

[英]How can I get extra data by Intent using Android annotation?

I'm really new to Android. 我真的是Android新手。 I'm trying to pass the data to the text view of another class. 我试图将数据传递给另一个类的文本视图。 But for some reason, it does not appear in the second class / page (the textview didnt change). 但是由于某种原因,它没有出现在第二堂课/页面中(textview并没有改变)。 I was able to do it when I was not using android annotation, but I was told that using annotations is a lot easier so I'm trying to convert it to that but for some reason it isn't working? 当我不使用android批注时,我能够做到这一点,但被告知使用批注要容易得多,因此我正尝试将其转换为该批注,但由于某种原因,它无法正常工作? I might be missing something. 我可能会缺少一些东西。

Main 1 主1

package com.example.faculty.labactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.world)
public class MainActivity extends AppCompatActivity {

    @ViewById(R.id.userName)
    EditText user;


    @Click(R.id.signIn)
    public void signInButton(){

     Main2Activity_.intent().name(user.getText().toString()).start();

   }

Main 2 主2

package com.example.faculty.labactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.welcome)
public class Main2Activity extends AppCompatActivity {

@Extra("name")
String n;

@ViewById(R.id.name)
TextView tv;

Intent in2 = getIntent();

@AfterViews
void home(){
    tv.setText("Hello, " + n + " !");
    }

}

In your receiving Activity (for example MyActivity) you must declare the name of the object you want to receive like that 在接收活动(例如MyActivity)中,您必须像这样声明要接收的对象的名称

@Extra("name")
String name;

To pass data, when you create your intent you must do something like this: 要传递数据,在创建意图时,您必须执行以下操作:

Main2Activity_.intent(this).name(user.getText().toString()).start();

If you need a more precise solution edit your question to show more of your code. 如果您需要更精确的解决方案,请编辑问题以显示更多代码。

You can look at the official doc for a more complete example 您可以查看官方文档以获取更完整的示例

It might have to do with the dependencies you should add for the annotations to your project_root/build.gradle and app/build.gradle files. 它可能与应该为project_root/build.gradleapp/build.gradle文件中的注释添加的依赖项有关。

Make sure to check this thread: what is Android Annotations and why we use Android Annotations? 确保检查此线程: 什么是Android注释,以及为什么我们使用Android注释?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM