【问题标题】:Passing Drawables between Activities在活动之间传递 Drawable
【发布时间】:2014-03-19 00:42:00
【问题描述】:

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

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

我应该怎么做才能使用 .putExtras(bundle) 在活动之间传递可绘制对象

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

}

【问题讨论】:

    标签: android android-intent android-activity android-drawable


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      只要它保持在同一个 apk 中,我只需将可绘制对象 (R.id.btn_picture) 的 id 传输为 int,因为所有服务/活动都可以获取资源。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-05
        • 2013-07-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多