0

我对 Java 和 Android 开发非常陌生,我正在尝试创建一个小“测验”,它RadioButton在每个视图上都有选择以及下一步和后退按钮。我正在使用下一步和后退按钮,但这似乎是一种非常冗长的方法。有没有一种方法可以定义一个函数并让它在该布局中工作,而不必每次使用后退按钮时都重新定义它。即我已经nexButtonGPAbuttonClick()在 中定义了,onCreate但是当我到达activity_main_2_.xml并将后退按钮设置为 时setContentView(activty_main.xml),我不得不在其中nextButtonGPAbuttonClick();再次重新定义它setContentView(R.layout.activity_main)。这是我的 .java 文件。任何指导都会非常有帮助。

package com.example.studentadvisorapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        nextButtonGPAbuttonClick(); 
    }

    private void nextButtonGPAbuttonClick() {
        //Get reference to button
        Button nextButtonGPA = (Button) findViewById(R.id.next_button_gpa);

        //set click listener
        nextButtonGPA.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setContentView(R.layout.activity_main_2);
            prevButtonHoursEnrolledbuttonClick();
            nextButtonHoursEnrolledbuttonClick();
        }

        private void prevButtonHoursEnrolledbuttonClick() {
            //Get reference to button
            Button prevButtonHoursEnrolled = (Button) findViewById(R.id.prev_button_hours_enrolled);

               //set click listener
           prevButtonHoursEnrolled.setOnClickListener(new View.OnClickListener() {
               @Override
              public void onClick(View v) {
                      setContentView(R.layout.activity_main);
                    nextButtonGPAbuttonClick();
              }
              });
           }

           private void nextButtonHoursEnrolledbuttonClick() {
             //Get reference to button
                Button nextButtonHoursEnrolled = (Button) findViewById(R.id.next_button_hours_enrolled);

             //set click listener
             nextButtonHoursEnrolled.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                setContentView(R.layout.activity_main_3);
                    nextButtonHoursWorkingbuttonClick();
                prevButtonHoursWorkingbuttonClick();
                 }

                 private void nextButtonHoursWorkingbuttonClick() {
                    //Get reference to button
                Button nextButtonHoursWorking = (Button) findViewById(R.id.next_button_hours_working);

                //set click listener
                nextButtonHoursWorking.setOnClickListener(new View.OnClickListener() {
                    @Override
                public void onClick(View v) {                setContentView(R.layout.activity_main_4);      
                }
            });
        }

        private void prevButtonHoursWorkingbuttonClick() {
            //Get reference to button
            Button nextButtonHoursWorking = (Button) findViewById(R.id.prev_button_hours_working);
            //set click listener
            nextButtonHoursWorking.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View v) {                                setContentView(R.layout.activity_main_2);                                     prevButtonHoursEnrolledbuttonClick();                            nextButtonHoursEnrolledbuttonClick();

            }
        });
    }                               
});
}
});
}




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

}
4

1 回答 1

1

启动一个新的 Activity 而不是调用 setContentView。这样后退按钮将自动将用户带回上一个屏幕。

于 2014-04-12T14:04:56.783 回答