gpt4 book ai didi

java - 辅助 Activity 的问题无法从 texView 获取数据(已解决 - Oleg Golomoz 回答)

转载 作者:行者123 更新时间:2023-12-01 19:41:50 25 4
gpt4 key购买 nike

您好,我的应用程序中有 2 个 Activity,我将从第一个 Activity 中获取数据并将其插入到第二个 Activity 中。

图片:我有 2 个日期选择器来选择日期范围,并将其保存在 texView 和 texView2 上。

enter image description here enter image description here

代码:主要 Activity

selectDate = (Button) findViewById(R.id.button); 日期 = (TextView) findViewById(R.id.textView);

    selectDate2 = (Button) findViewById(R.id.button2);
date2 = (TextView) findViewById(R.id.textView2);

selectDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
date.setText("Entrada: "+day + "/" + (month + 1) + "/" + year);
}
}, year, month, dayOfMonth);
datePickerDialog.show();
}
});


selectDate2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calendar2 = calendar2.getInstance();
year2 = calendar2.get(Calendar.YEAR);
month2 = calendar2.get(Calendar.MONTH);
dayOfMonth2 = calendar2.get(Calendar.DAY_OF_MONTH);
datePickerDialog2 = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
date2.setText("Salida: "+day + "/" + (month + 1) + "/" + year);
}
}, year2, month2, dayOfMonth2);
datePickerDialog2.show();
}
});

Activity 2:

package com.example.mand.consumirapirest;

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

public class json extends AppCompatActivity {

TextView tvJSON;
TextView textView;

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

public void setTextView() {
tvJSON = (TextView) findViewById(R.id.tvJSON);
textView = (TextView) findViewById(R.id.textView);

String s = textView.getText().toString();

tvJSON.setText(s);


//String text = textView.getText().toString();
//return text;
}

/*public String getText()
{

}*/
}

任何想法谢谢..!

最佳答案

第一个 Activity 中的某个位置 (MainActivity.java):

Button button = findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);

// Pass some data
// Key "DATE_KEY" is static field from SecondActivity
intent.putExtra(DATE_KEY, "20.03.2019");

startActivity(intent);
}
});

在第二个 Activity (SecondActivity.java) 中:

public class SecondActivity extends AppCompatActivity {

static final String DATE_KEY = "DATE_KEY";

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

// Read passed data - String "20.03.2019"
String date = getIntent().getStringExtra(DATE_KEY);
}
}

关于java - 辅助 Activity 的问题无法从 texView 获取数据(已解决 - Oleg Golomoz 回答),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55117155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com