android - 具有多个配置 Activity 的一个小部件 (Android)

标签 android configuration android-activity widget

我想创建一个小部件应用程序,添加后将启动 ConfigurationActivity,其中有一些选项可供选择。

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

然后,单击Finish 按钮并返回到现在显示小部件的主屏幕。

这可以通过我在 XML 中定义的配置 Activity 来完成,还是我必须在 onEnabled() 中执行 startActivity 然后更新我的小部件那样吗?

感谢您的帮助。

最佳答案

您完全可以通过在 xml 中定义的配置 Activity 来完成此操作。只需让您的第一个 Activity 启动一个进入您的第二个 Activity 的 Intent ,但使用 startActivityForResult()方法。然后在您的第二个 Activity 中,当用户在第二个 Activity 中单击“完成”按钮时,第二个 Activity 调用 finish() 方法。但在您调用 finish 之前,将结果设置为您在第二个 Activity 中收集的所有数据。然后控件将返回到第一个 Activity ,您可以在 onActivityResult() 方法中处理从第二个 Activity 获得的结果。然后只需将第二个 Activity 的结果添加到您要从此 Activity 返回的结果中。

好吧,让我们看一个简单的示例。

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


}

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

关于android - 具有多个配置 Activity 的一个小部件 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7726982/

相关文章:

c# - .NET 自定义配置部分 : Configuration. GetSection 引发 'unable to locate assembly' 异常

android - 如何旋转ImageView并自动调整其大小?

java - Android 应用程序中方法 Picasso.with().load() 中的目标不为 null

android: 首选项 xml 中的可见性属性不起作用? (安卓 2.3)

android - RecylerView Adapter 负责什么?选择以下每个正确的语句 :

android - 单击按钮打开 SettingsActivity 的子首选项屏幕

ssl - Nginx 条件客户端证书身份验证

python - matplotlib 的 rcParams 中的 "rc"代表什么?

java - 我应该在 MainActivity 中定义 SharedPreferences 吗?

android - <activity-alias> 打破了 singleTop 模式的契约