将文本值传递给另一个文本

isr3a4wc  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(245)

我想使用java在android studio中构建一个类别列表滚动视图。我必须了解如何在其他活动中以相同的值将值从textview传递到Antor textview。
activity_first.xml

<TextView
            android:id="@+id/PersonName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:textColorHint="@color/black"
            android:background="@android:color/transparent"
            android:text="Name" />

firstactivity.java

public class FirstActivity extends AppCompatActivity {
    TextView PersonName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        PersonName = (TextView)findViewById(R.id.PersonName);

        PersonName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent p = new Intent(FirstActivity.this, SecondActivity.class);
                startActivity(p);
            }
        });
    }
}

在第二个活动中,我有一个滚动视图。
activity_second.xml

<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Father" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Mother" />
            </LinearLayout>
        </ScrollView>

在activity_second.xml中,当我按下textview时,它会将我的值father(id:textview)返回到firstactivity,并将textview(id:personname)更改为father,如果我使用值mother(id:textview2)执行此操作,它会执行相同的操作并将我返回到firstactivity。

bjp0bcyl

bjp0bcyl1#

您可以使用getextra方法来完成
谷歌安卓getextra
secondactivity.java

public class SecondActivity extends AppCompatActivity {

    TextView mother,father;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second.xml);

        mother = (TextView)findViewById(R.id.textView);
        father = (TextView)findViewById(R.id.textView2);

        mother.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("id", seciliodaid);
                bundle.putInt("type", 0);
                Intent newIntent;
                newIntent = new Intent(SecondActivity.this, FirstActivity.class);
                newIntent.putExtras(bundle);
                startActivityForResult(newIntent, 0);
            }
        });

 father.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("id", seciliodaid);
                bundle.putInt("type", 1);
                Intent newIntent;
                newIntent = new Intent(SecondActivity.this, FirstActivity.class);
                newIntent.putExtras(bundle);
                startActivityForResult(newIntent, 0);
            }
        });
    }
}

firstactivity.java

public class FirstActivity extends AppCompatActivity {
    TextView PersonName;
    Bundle data;
    int type=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        PersonName = (TextView)findViewById(R.id.PersonName);

        try {
                data = getIntent().getExtras();
                type = data.getInt("type");
                if(type==0){
                  PersonName.setText("Mother");
                }else if(type==1){
                  PersonName.setText("Father");
                }
         }catch (Exception e){
         }

        PersonName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent p = new Intent(FirstActivity.this, SecondActivity.class);
                startActivity(p);
            }
        });
    }
}
bxgwgixi

bxgwgixi2#

虽然你可以用 Bundle 要做到这一点(如另一个答案所示),如果您只需要字符串,那么您可以执行以下操作(我认为这更简单,但取决于您的实现):
在按钮 onClickListener firstactivity.java的名称:

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("Mother", mother.getText().toString());
intent.putExtra("Father", father.getText().toString());

在文本视图中 onClickListener secondactivity.java的名称:

String mother = getIntent().getStringExtra("Mother");
String father = getIntent().getStringExtra("Father");

相关问题