[英]How to change a value to an Activity after pressing a button in android?
(Edited to be able to compare string) I have 1 TextView, and 2 buttons, one button is "Month" the other button "Week". (编辑能够比较字符串)我有1个TextView和2个按钮,一个按钮是“月”,另一个按钮是“周”。 Im trying to change the Textview accordingly to the button pressed eg Month,Week When i start the activity for the first time it displays the "Month" as expected. 我试图将Textview相应更改为按下的按钮,例如月,周当我第一次启动活动时,它会按预期显示“月”。
After when i press "Week" button always displays the "Week" in the TextView, even if i click on "Month" button still shows Week view. 按下“周”按钮后,总是在TextView中显示“周”,即使我点击“月”按钮仍然显示周视图。
debugging says that when i press "Month" , "Month"= "true", then onCreate the value is still "True", but in the 1st If statement 调试说当我按“月”,“月”=“真”时,则onCreate值仍为“True”,但在第1个If语句中
if (extras != null){ // <-------here is still true
month = extras.getString(month); // <-------here is false
}
that value suddenly goes to "false" 那个价值突然变成了“假”
I know i could settext in the buttons, but later on i will add graphs to display data, so i would like to be done onCreate. 我知道我可以在按钮中设置文本,但稍后我将添加图形来显示数据,所以我想在创建上完成。 Every times it creates the view, will check which view is selected(by comparing the string) and display the message and graphs. 每次创建视图时,都会检查选择哪个视图(通过比较字符串)并显示消息和图形。 first time run to display the Month view. 第一次运行以显示月视图。
What am i doing wrong? 我究竟做错了什么?
Heres the code 继承人的代码
package com.isma.report;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class report1 extends Activity{
TextView viewtime;
String month = "true";
Intent starterIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
starterIntent = getIntent();
Bundle extras = getIntent().getExtras();
if (extras != null) {
month = extras.getString("month");
}
// Set the text
viewtime = (TextView) findViewById(R.id.StudentReportText);
if (month.equals("true")){
viewtime.setText("Monthly View");
}
if{month.equals("false")){
viewtime.setText("Weekly View");
}
//Month View Button
Button bMonth = (Button) findViewById(R.id.monthincome);
bMonth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
starterIntent.putExtra(month, "true");
startActivity(starterIntent);
finish();
}
});
//Week View Button
Button bWeek = (Button) findViewById(R.id.weekincome);
bWeek.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
starterIntent.putExtra(month, "false");
startActivity(starterIntent);
finish();
}
});
}
}
if (month == "true")
You can't compare strings in Java using ==
. 您无法使用==
来比较Java中的字符串。 Use equals(...)
- example... 使用equals(...)
- 示例......
if (month.equals("true"))
Finally i managed to fix it! 最后我设法解决了!
First thanks to all u helped me. 首先,谢谢你帮助我。
Now the fix is very simple. 现在修复非常简单。
I moved the initialization of the String month to inside of onCreate method with no value. 我将String月的初始化移动到onCreate方法的内部,没有任何值。 i also extended the if statement to assign the value true if was the firs time running or not pressing any button. 如果第一次运行或没有按任何按钮,我还扩展了if语句以赋值true。
String month = "";
Bundle extras = getIntent().getExtras();
if (extras != null) {
month = extras.getString(month);
}
else{
month = "true";
}
I also initialized with no values the month variable in the onClick methods 我还初始化了onClick方法中没有值month变量的值
String month = "";
very simple but it took me 2 days to figure out! 非常简单,但我花了2天才弄明白! ;p thanks again guys ;再次感谢你们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.