gpt4 book ai didi

android - 在 Activity 之间传递 Drawable

转载 作者:行者123 更新时间:2023-11-29 21:11:57 25 4
gpt4 key购买 nike

我正在尝试在 Activity 之间传递可绘制对象。我不确定是否可以从 alertdialog 中获取可绘制资源 ID 并将该值作为 Int 传递给另一个 Activity 。

我探索过将可绘制对象更改为位图,我也尝试过 getResources().getIdentifier() 但也许我使用不正确。

我应该怎么做才能使用 .putExtras(bundle) 在 Activity 之间传递 drawable

public class CreateActivity extends Activity implements OnClickListener {

EditText etTitle;
Button btDate;
Button btTime;
Button btPic;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

//onclicklistener
findViewById(R.id.btn_confirm).setOnClickListener(this);
findViewById(R.id.btn_back).setOnClickListener(this);

etTitle = (EditText) findViewById(R.id.editTextTitle);
btDate = (Button) findViewById(R.id.btn_date);
btTime = (Button) findViewById(R.id.btn_time);
btPic = (Button) findViewById(R.id.btn_picture);

}

// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_confirm:
String title = etTitle.getText().toString();
String time = btTime.getText().toString();
String date = btDate.getText().toString();

Drawable drawable = getResources().getDrawable(R.id.btn_picture);

Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

Log.e("LOG", title);
Log.e("LOG", time);
Log.e("LOG", date);

Bundle newBundle = new Bundle();
newBundle.putString("TITLE", title);
newBundle.putString("TIME", time);
newBundle.putString("DATE", date);

//Trying to pass a drawable from one activity to another
newBundle.putParcelable("BITMAP", bitmap);

Intent intent = new Intent(this, MainActivity.class);
intent.putExtras(newBundle);

setResult(RESULT_OK, intent);

finish();

break;

case R.id.btn_back:
finish();
break;
}

}

public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}

public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}

public void showPicturePickerDialog(View v) {
DialogFragment newFragment = new PicturePickerFragment();
newFragment.show(getFragmentManager(), "picturePicker");
}

最佳答案

我建议您传递标识符,它只是一个整数。在下一个 Activity 中根据需要重建可绘制对象。

intent.putIntExtra(DRAWABLE_KEY, R.id.your_drawable_id);

关于android - 在 Activity 之间传递 Drawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22494022/

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