【发布时间】:2013-02-02 11:55:48
【问题描述】:
我的一个布局中有一个无边框按钮。单击它时,我希望意图传输按钮的标签及其包含的文本。我很难做到这一点。
这是我点击按钮时的效果:
public void openGoalWeek (View view) {
Intent intent = new Intent(this, ViewGoal.class);
Button button = (Button) findViewById(R.id.week_goal);
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MESSAGE, button.getText().toString());
bundle.putString(EXTRA_TAG, button.getTag().toString());
intent.putExtras(bundle);
startActivity(intent);
}
这是我在ViewGoal 类中的内容:
public class ViewGoal extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_goal);
// make sure running on honeycomb or higher for actionbar API
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
// get the message from the intent
Intent intent = getIntent();
String message = intent.getBundleExtra(MainActivity.EXTRA_MESSAGE).toString();
String tag = intent.getBundleExtra(MainActivity.EXTRA_TAG).toString();
String text = "null";
if (tag == "year_tag") {
DatabaseHandler db = new DatabaseHandler(this);
Goal goal = db.getYearGoal(message);
text = goal._title + "\n" + goal._description;
}
if (tag == "month_tag") {
DatabaseHandler db = new DatabaseHandler(this);
MonthGoal goal = db.getMonthGoal(message);
text = goal._title + "\n" + goal._description;
}
if (tag == "week_tag") {
DatabaseHandler db = new DatabaseHandler(this);
WeekGoal goal = db.getWeekGoal(message);
text = goal._title + "\n" + goal._description;
}
// create the text view
TextView textView = new TextView(this);
textView.setTextSize(10);
textView.setText(text);
// display the content
setContentView(textView);
}
}
它向我抛出一个错误,我不知道为什么。任何帮助表示赞赏!
【问题讨论】:
-
什么错误?发布 logcat 文件。
-
好的,我已经摆脱了错误。我没有在 xml 文件中正确设置标签。但是现在当我运行应用程序时,它不会更改字符串文本。它只是保持为空,即使其中一个 if 语句为真。
-
Put Log.i("TAG", variable);在if条件下。因此,您将了解您尝试设置的该标签的实际值。
-
好的,问题是 if 语句没有执行。如果我放置“Log.i("TAG", variable);"在 if 语句中,没有任何输出。如果我把它放在它们外面,它会打印出标签。比如我刚刚运行,标签是“年份”,Log.i语句打印出标签,但是if语句没有执行。我相信我的设置正确吗?
-
将语句更改为 if (tag.equals(nameOfString)) 或 if (tag.equalsIgnoreCase(nameOfString))。 == 不适用于字符串。
标签: android android-intent android-button