如何根据活动的变化更改textview

时间:2013-12-13 21:51:31

标签: android android-activity

我是android开发的新手。我正在进行一个有10个问题(10个活动)的测验。还有另一个活动(名称“Lost”),根据问题编号(活动编号)打开错误答案并包含TextView(现在告诉分数)。我无法根据活动编号设置TEXTV视图。例如,如果一个人在第5次失败时将活动(丢失)打开并将文本视图设置为50,并且当人在第9个问题时给出错误答案时,活动(丢失)将打开并将文本视图设置为90.我不知道如何这样做..帮助我

这是我的代码 -

这是第一个问题(类似的代码用于其他问题) -

    RadioGroup rg1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level1);
        // Show the Up button in the action bar.
        setupActionBar();
        rg1=(RadioGroup)findViewById(R.id.rg1);
        rg1.setOnCheckedChangeListener(this);

    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.level1, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCheckedChanged(RadioGroup arg0, int arg1) {
        // TODO Auto-generated method stub
        switch(arg1){
        case R.id.r11:
        RadioButton ans=(RadioButton)findViewById(R.id.r11);
            ans.setOnClickListener(new View.OnClickListener(){
                public void onClick(View view){
                    Intent myintent=new Intent(view.getContext(), Level2.class);
                    startActivityForResult(myintent,0);

                }
            });
            break;
        case R.id.r12:
            RadioButton ans1=(RadioButton)findViewById(R.id.r12);
            ans1.setOnClickListener(new View.OnClickListener(){
                public void onClick(View view){
                    Intent myintent=new Intent(view.getContext(), Lost.class);
                    startActivityForResult(myintent,0);

                }
            });

        case R.id.r13:RadioButton   ans2=(RadioButton)findViewById(R.id.r13);
        ans2.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Intent myintent=new Intent(view.getContext(), Lost.class);
                startActivityForResult(myintent,0);

            }
        });

            break;
        case R.id.r14:RadioButton   ans3=(RadioButton)findViewById(R.id.r14);
        ans3.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Intent myintent=new Intent(view.getContext(), Lost.class);
                startActivityForResult(myintent,0);

            }
        });
            break;
        }
    }

这是“迷失”活动代码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lost);
        // Show the Up button in the action bar.
        setupActionBar();
        Button home=(Button)findViewById(R.id.Home);
        home.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Intent myintent=new Intent(view.getContext(), MainActivityQuiz.class);
                startActivityForResult(myintent,0);

            }
        });

    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.lost, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

请告诉我该怎么做....

1 个答案:

答案 0 :(得分:1)

在您的关卡活动中添加额外的意图

        Intent myintent=new Intent(view.getContext(), Lost.class);
        myintent.putExtra("score", LEVEL_SCORE);
        startActivityForResult(myintent,0);

其中LEVEL_SCORE是与您的关卡相关联的int得分。 在失去的活动中,你可以得到这样的分数:

        Intent myIntent = getIntent();
        int score = myIntent.getIntExtra("score");

您现在可以在textView中显示分数。