Android - 使用“startActivityonResultFor”

时间:2017-11-13 02:52:01

标签: android onactivityresult

我试图将其设置为当有人点击默认头像图片时,它会将它们发送到一个单独的活动,其中包含一系列可供选择的头像。用户将选择其中一个头像,然后我想将用户发送回主要活动,将头像ImageView设置为他们选择的头像。我用文字完成了这个,但没有图像,我已经达到了混乱的程度。任何人都可以帮我做到这一点吗?

// First View
public class MainActivity extends AppCompatActivity {
    ImageView imageViewSelectAvatarLocal;
    int requestCodeImageBrownHairedFemale = 100;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageViewSelectAvatarLocal = findViewById(R.id.imageViewdefaultAvatar);
        imageViewSelectAvatarLocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SelectAvatarActivity.class);
                intent.putExtra("Brown Haired Woman", 001);
                startActivityForResult(intent, 100);
            }
        });
    }

    @SuppressLint("ResourceType")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == SelectAvatarActivity.RESULT_OK && data != null){
            if(requestCode == requestCodeImageBrownHairedFemale) {
                //...
            }
        }
    }
}
// Second View
public class SelectAvatarActivity extends AppCompatActivity {
    ImageView imageViewBrownHairedWoman
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_avatar);
        imageViewBrownHairedWoman = findViewById(R.id.imageViewBrownHairFemale);
        imageViewBrownHairedWoman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.getExtras();
                setResult(MainActivity.RESULT_OK, intent);
                finish();
            }
        });

    }
}

1 个答案:

答案 0 :(得分:1)

使用数组怎么样? 你有一些头像图片。所以你可以做到

int[] avatars = {R.drawable.avatar0, ... };

在第二个视图中,单击其中一个头像图像(每个图像都有位置),将位置发送到MainActivity并使用onActivityResult方法获取值。

例如,

public class MainActivity extends AppCompatActivity {
    int[] avatars = {R.drawable.avatar0, ... };
    ImageView imageViewSelectAvatarLocal;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageViewSelectAvatarLocal = findViewById(R.id.imageViewdefaultAvatar);
        imageViewSelectAvatarLocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SelectAvatarActivity.class);
                startActivityForResult(intent, 100);
            }
        });
    }

    @SuppressLint("ResourceType")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == SelectAvatarActivity.RESULT_OK && data != null){
            if(requestCode == 100) {
              int pos = data.getIntExtra("avatar", 0);
              imageViewSelectAvatarLocal.setImageResource(avatars[pos]);
            //...
            }
        }
    }
}

// Second View
public class SelectAvatarActivity extends AppCompatActivity {
    ImageView avatar0, avatar1, ...;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_avatar);
        avatar0 = findViewById(R.id.avatar1);
        avatar0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("avatar", 0);
                setResult(MainActivity.RESULT_OK, intent);
                finish();
            }
        });

        //...
    }
}