0

我的一个布局中有一个无边框按钮。单击它时,我希望意图传输按钮的标签及其包含的文本。我很难做到这一点。

这是单击按钮时我所拥有的:

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);
    }
}

它向我抛出了一个错误,我不知道为什么。任何帮助表示赞赏!

4

2 回答 2

0

你为什么不这样做,只是把你的额外而不是使用捆绑:

...
Intent intent = new Intent(this, ViewGoal.class);
intent.putExtra( EXTRA_MESSAGE, button.getText().toString());
intent.putExtra( EXTRA_TAG, button.getTag().toString());
....

稍后在其他活动中:

....
// get the message from the intent
Intent intent = getIntent();
String id   = intent.getStringExtra(EXTRA_MESSAGE);
String name = intent.getStringExtra(EXTRA_TAG);
....
于 2013-02-18T05:11:36.847 回答
0

在 onClick 按钮中,而不是这个使用classname.this 了我的一些错误被这个删除了。试试看对你有没有帮助。

public void openGoalWeek (View view) {
Intent intent = new Intent(className.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);
于 2013-02-18T05:14:21.260 回答