一个Android类可以处理多个活动以及如何开始正确的活动吗?

时间:2012-09-03 07:56:29

标签: android android-layout android-intent

我特别需要"设置"设置,我有4个选项(年龄,身高,体重和性别)列表。现在,每个列表项必须打开新的"查看" (活动,窗口),它实际上没有做太多。 EG,性别是单选按钮和" OK"按钮,然后将性别保存在共享首选项中。

所以,我有很好的想法来创建"设置"类,然后以某种方式处理这个。当选择第1项时,它将显示layout1.xml并从Settings类调用function1。

Item2调用layout2.xml并从Settings类调用function2。

这是一种好的方法,还是我应该为每一帧创建类,并且有一些小的" GenderSelector"实际上只做处理单选按钮的类?

1 个答案:

答案 0 :(得分:1)

  1. 您可以使用dialogs
  2. 您用来启动活动的意图可以包含可以为正在启动的活动访问的原始数据。然后,您可以使用控件有条件地填充已启动的活动。毕竟,您可以将结果返回到第一个活动。
  3. 使用指定模式启动活动的代码片段(模式常量定义如下):

    public class SettingsActivity extends Activity
    {
        public static final String EXTRA_MODE = "your_mode_extra_name";
        public static final int MODE_AGE = 0;
        public static final int MODE_GENDER = 1;
    
        private static final int MODE_DEFAULT = MODE_AGE;
    
        @Override
        public void onCreate(Bundle savedInstance)
        {
            int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_DEFAULT);
    
            switch(mode)
            {
                case MODE_AGE:
                    setContentView(R.layout.age_mode_layout);
                    //TODO: find controls by ID and set their event handlers here
                    break;
    
                 case MODE_GENDER:
                    setContentView(R.layout.age_mode_gender);
                    //TODO: find controls by ID and set their event handlers here
                    break;
            }
        }
    }
    

    返回性别的代码段:

    public class SettingsActivity extends Activity
    {
        public static final String EXTRA_GENDER = "your_gender_extra_name";
    
        public static final int GENDER_FEMALE = 0;
        public static final int GENDER_MALE = 1;
    
        private void returnGender(int gender)
        {
            setResult(RESULT_OK, new Intent().putExtra(EXTRA_GENDER, gender));
            finish();
        }
    }
    

    最后显示设置活动并返回结果:

    public class MainActivity extends Activity
    {
        //actually we could use mode constants from SettingsActivity;
        //just to show that generally this is separate constants set
        private static final int ID_AGE = 0;
        private static final int ID_GENDER = 1;
    
        private void requestAge()
        {
            Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
            intent.putExtra(SettingsActivity.EXTRA_MODE, SettingsActivity.MODE_AGE);
    
            startActivityForResult(intent, ID_AGE);
        }
    
        private void requestGender()
        {
            Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
            intent.putExtra(SettingsActivity.EXTRA_MODE, SettingsActivity.MODE_GENDER);
    
            startActivityForResult(intent, ID_GENDER);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            if (resultCode != RESULT_OK)
                return; //something went wrong
    
            switch(requestCode)
            {
                case ID_GENDER:
                    int gender = data.getIntExtra(SettingsActivity.EXTRA_GENDER, -66666);
                    //you've got the gender, use it
                    break;
    
                case ID_AGE:
                    //get age from the intent and use it
                    break;
            }
        }
    }