2011-10-11 70 views
1

我想创建一个小部件应用程序,当添加时将开始ConfigurationActivity,它有一些选项可供选择。具有多个配置活动的一个小工具(Android)

然后,您可以单击Next按钮以转到NextActivity并配置您在第一个屏幕上选择的每个单独选项。

然后,点击Finish按钮,并返回主屏幕,其上带有小部件。

这可以从配置的活动,我定义我的XML,或做我必须做的,也许在onEnabled()一个startActivity并然后更新我的窗口小部件的方式进行?

谢谢你的帮助。

回答

3

您完全可以从您在xml中定义的配置活动完成此操作。只是让你的第一个活动开始一个意图,你的第二个活动,但使用startActivityForResult()方法。然后在第二个活动中,当用户在第二个活动中单击完成按钮时,使第二个活动调用finish()方法。但在致电完成之前,请将结果设置为您在第二个活动中收集的所有数据。控制将返回到第一个活动,您可以在onActivityResult()方法中处理从第二个活动中获得的结果。然后,将第二个活动的结果添加到您要从此活动返回的结果中。

好吧,让我们来看看这个准系统的一个例子。

ConfigActivity1 extends Activity{ 

    protected onCreate(Bundle icicle){ 
    //do your setup stuff here. 

    //This is the button that's going to take us to the next Config activity. 
    final Button nextConfig = (Button)findViewById(R.id.next_config); 
    //We'll add an onClickListener to take us to the second config activity 
    nextConfig.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View view){ 
     //Construct Intent to launch second activity 
     Intent furtherConfigIntent = new Intent(ConfigActivity1.this, ConfigActivity2.class); 
     //By using startActivityForResult, when the second activity finishes it will return to 
     //this activity with the results of that activity. 
     startActivityForResult(furtherConfigIntent, 0); 
     } 
    }); 
    //finish any other setup in onCreate 
    } 

    //This is a callback that will get called when returning from any activity we started with the 
    //startActivityForResult method 
    protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
    if(resultCode == Activity.RESULT_CANCELED){ 
     //this means the second activity wasn't successfull we should probably just 
     //return from this method and let the user keep configuring. 
     return; 
    } 
    //ok, if we made it here, then everything went well in the second activity. 
    //Now extract the data from the data Intent, compile it with the results from this 
    //acitivity, and return them. Let's say you put them in an Intent called resultsIntent. 
    setResult(Activity.RESULTS_OK, resultsIntent); 
    finish(); 
    } 


} 

第二个acitvity会非常简单。只需收集你的配置数据,当用户按下完成时,将结果数据和resultCode设置为OK,然后完成。

+0

谢谢。它看起来像这回答我的问题。 :) – Jakar

+0

问题是关于一个小部件。但答案是关于一个应用程序(这更容易)。所以我不明白这是如何回答这个问题的。 –

+0

@BobUeland问题是相当开放的,但它确实说“我的小工具应用程序”,我觉得给了我一个相当广泛的问题来回答它的许可证。提问者似乎也认为我回答了这个问题。 如果您希望获得更具体的内容,可能会提出一个新问题。 –