Android - 使用 "startActivityonResultFor" 从另一个 ImageView 接收图像
Android - Receiving image from another ImageView with "startActivityonResultFor"
我正在努力做到这一点,当有人点击默认头像图片时,它会将他们发送到一个单独的 activity,其中将包含一个 select 离子头像可供选择。用户将 select 这些化身之一,然后我想将用户发送回 Main activity,并将化身 ImageView 设置为他们 select 编辑的那个。我用文字做了这个,但没有用图像,我已经到了困惑的地步。谁能帮我实现这个目标?
// 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();
}
});
}
}
使用数组怎么样?
你有一些头像图片。所以你可以做
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();
}
});
//...
}
}
我正在努力做到这一点,当有人点击默认头像图片时,它会将他们发送到一个单独的 activity,其中将包含一个 select 离子头像可供选择。用户将 select 这些化身之一,然后我想将用户发送回 Main activity,并将化身 ImageView 设置为他们 select 编辑的那个。我用文字做了这个,但没有用图像,我已经到了困惑的地步。谁能帮我实现这个目标?
// 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();
}
});
}
}
使用数组怎么样? 你有一些头像图片。所以你可以做
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();
}
});
//...
}
}