之前我问过这个问题,但我不会解释发生了什么,而是要问你将如何做到这一点。
我的目标:
我有2个屏幕。
屏幕1:
(textview) - > “选择你喜欢的号码”
(旋转器) - >下拉数字列表以供选择
(btnGo) - >启动新意图并更改为屏幕2
屏幕2:
“你最喜欢的号码是:(无论你在锭床上选择什么)”
我尝试了不同的方法但是在屏幕更改后我无法从微调器中检索信息。
那你怎么做? 请尽可能详细。
答案 0 :(得分:0)
你还没有解释你的“屏幕”是什么,所以我会假设它们是活动。
如果要将数据从一个活动传递到另一个活动,而不是尝试反向引用调用第二个活动的活动,则应将其作为意图的一部分所需的任何数据传递给第二个活动。
E.g:
myIntent.putExtra("spinnerValue", spinner.getIndex());
在第二个活动中,您可以阅读数据:
int spinnerValue = getIntent().getExtras().getInt("spinnerValue");
答案 1 :(得分:0)
your_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapter, View v, int i, long lng) {
selected_number = adapter.getItemAtPosition(i).toString();
}
}
Intent intent = new Intent(screen1.this, screen2.class);
intent.putExtra("selectedNumber", selected_number);
startActivity(intent);
在屏幕2中:
String number = this.getIntent.getExtras("selectedNumber");
your_textView.setText(number);
答案 2 :(得分:0)
基本上当你打电话给SCREEN2我假设你运行一个新意图,它会删除你存储的所有数据。
你应该做的是使用共享偏好here是一个教程
或者您可以制作两个布局,将第一个设置为visible
,将另一个设置为invisible
,这样当您单击btnGO时,隐藏包含微调器和按钮并显示的第一个布局包含文本的新布局。
修改强>
首先确保(textview)(微调器)(btnGo)有一个单独的布局,例如LinearLayout,然后你最喜欢的数字的另一个布局是:(无论你在微调器上选择什么)“只将它们放在一个xml文件上,或者如果您愿意,可以使用<include />
如果您将它们放在单独的xml文件中,并在执行按钮单击时设置可见性
spinner.setVisibility(View.INVISIBLE);
btnGo.setVisibility(View.INVISIBLE);
textView.setVisibility(View.INVISIBLE);
"Your favorite number is:(whatever you selected on thespinner)".setVisibility(View.VISIBLE);
你明白了吗?
答案 3 :(得分:0)
你可以像这样使用:
在按钮上的屏幕-1中单击:
Intent intent = new Intent(getApplicationContext(),SCREEN2.class);
intent.putExtra("favoriteNumber",GET_SELECTED_SPINNER);
startActivity(intent);
在Screen-2 onCreate()中:
String selectedFavorite = getIntent().getStringExtra("favoriteNumber");
YOUR_TEXTVIEW.setText(selectedFavorite);
那就是它。
希望它对你有用。
答案 4 :(得分:0)
//在screen1按钮事件中
Screen2 sc2 = new Screen2();
sc2.Set(3);
// <强> ** 强>
// Screen2类
类Screen2 {
private int i;
public int Get(){
return i;
public void Set( int a )
this.i = a;
}