gpt4 book ai didi

java - 如何单击 ImageViews 和另一个图像显示在来自 Drawable 的另一个 Activity 的 TextView 上

转载 作者:行者123 更新时间:2023-12-02 05:34:30 24 4
gpt4 key购买 nike

enter image description here

实际上我正在制作一个Pin Lock Screen应用程序,并且我只做了两个 Activity 。在 Activity1 上,我希望如果我单击 On ImageView then OnActivity2 另一张图像从drawable显示

enter image description here

TextView 上,它保存在 SharedPreference 上,我是一个初学者,请提供任何帮助,我会陷入困境。

第一个 Activity

public class MainActivity extends Activity implements View.OnClickListener{
ImageView mpink,mpurple;
int view;
String display;
private ImageView results;
private Object get_input;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mpink=(ImageView) findViewById(R.id.imageView);
mpurple=(ImageView) findViewById(R.id.imageView2);

mpink.setOnClickListener(this);
mpurple.setOnClickListener(this);

}

@Override
public void onClick(View v) {
if (results == mpurple) {
display = "get_input";
view = R.drawable.comb1;
}
else if (results == mpink) {
display = "get_input";
view = R.drawable.comb2;
}
else {
display = "get_input";
view = R.drawable.comb3;
}
Intent home_intent=new Intent(getApplicationContext(),Main2Activity.class);
home_intent.putExtra("home_store_image_src", (Parcelable) mpink);
home_intent.putExtra("home_store_image_src", (Parcelable) mpurple);
startActivity(home_intent);
}}

第二个 Activity

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

get_result=(TextView)findViewById(R.id.displayresult);
Intent result_intent=getIntent();
String text=result_intent.getStringExtra("get_input");
get_result.setText(text);// display what the user has enter

// get_image.setImageResource
//OR other coding to display imageview by using the image_id from activty 1?
int defaultIdWhenCouldNotFindImageSourceId = -1;//should use a negative value
int imageId = result_intent.getIntExtra("home_store_image_src", defaultIdWhenCouldNotFindImageSourceId);
if( defaultIdWhenCouldNotFindImageSourceId != imageId) {
//when you can find an valid image id in intent extra, display it
get_image.setImageResource(imageId);
}
}}

最佳答案

请检查此问题以帮助您将可绘制对象从一个 Activity 发送到另一个 Activity

How to pass drawable between activities

所以当你使用onClick

 @Override
public void onClick(View v) {
Bitmap bitmap;
if (results == mpurple) {
display = "get_input";
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.comb1);
}
else if (results == mpink) {
display = "get_input";
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.comb2);
}
else {
display = "get_input";
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.comb3);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();

Intent home_intent=new Intent(getApplicationContext(),Main2Activity.class);
home_intent.putExtra("picture", b);
home_intent.putStringExtra("getInput", display);
startActivity(home_intent);
}}

此外,您需要编辑第二个 Activity 并将 ImageView 放入其 XML 中,您无法将 ImageView 从 Activity 传递到另一个 Activity,但可以传递Drawable 并将其设置为第二个 Activity 中的 ImageView

关于java - 如何单击 ImageViews 和另一个图像显示在来自 Drawable 的另一个 Activity 的 TextView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56180802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com