意图 - 转移按钮标签?

时间:2013-02-18 04:59:32

标签: android android-intent android-button

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

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

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

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

2 个答案:

答案 0 :(得分:0)

为什么你不这样做,只需把你的EXTRA而不是使用bundle:

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

答案 1 :(得分: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);