从Button中检索Drawable的ResourceId

时间:2014-03-24 00:26:47

标签: android button android-intent drawable

我需要做些什么才能获得设置为按钮的drawable的resId?

FLOW

用户创建活动>>用户点击"图片按钮" >>用户提示AlertDialog,其中包含一系列drawables>>用户选择Drawable>> Drawable设置为"图片按钮" >>用户按下"完成按钮>> drawable resID应该使用putInt()从CreateActivity传递给MainActivity

我似乎无法弄清楚如何获得设置为"图片按钮"的drawable的resId。通过PicturePickerFragment中的alertdialog。

我尝试过使用getCompoundDrawable(),但我不确定我是否错误地实现了它。

在CreateActivity中

// 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();

        btPic.getCompoundDrawables();
        int resId = getResources().getIdentifier("com.example.datetracker:drawable/baby", null, null);;

        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.putInt("DRAWABLE", resId);

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

        setResult(RESULT_OK, intent);

        finish();

        break;

在PicturePickerFragment中

// defining listview and using array adapter
    listView = (ListView) convertView.findViewById(R.id.listViewFragment2);
    DrawableAdapter adapter = new DrawableAdapter(getActivity(), rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, final View view,
                int position, long id) {

            // final int item = images[position];
            Drawable myDrawable = getResources().getDrawable(
                    images[position]);
            Button b = (Button) getActivity()
                    .findViewById(R.id.btn_picture);
            b.setCompoundDrawablesWithIntrinsicBounds(null, myDrawable,
                    null, null);
        }

    });
MainActivity中的

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Create the adapter to convert the array to views
    EventAdapter adapter = new EventAdapter(this, lstEvents);

    // attach adapter to a list view
    listView = (ListView) findViewById(R.id.listViewFragment);
    listView.setAdapter(adapter);

    if (requestCode == 100) {
        if (resultCode == RESULT_OK) {
            Bundle b = data.getExtras();
            title = b.getString("TITLE");
            time = b.getString("TIME");
            date = b.getString("DATE");

            // retrieving resId, creating drawable

            resId = b.getInt("DRAWABLE");
            Log.e("RESIDRESIED", "DRAWABLE " + resId);
            String resName = getResources().getResourceName(resId);
            Log.e("RESIDRESIED", "DRAWABLE " + resName);

1 个答案:

答案 0 :(得分:0)

我在创建活动中使用以下内容从按钮

中检索drawable
Drawable drawable = btPic.getCompoundDrawables()[1];
Log.e("DRAWABLE", "DrawableId" + drawable);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

我用它将它放入一个包

newBundle.putParcelable("DRAWABLE", bitmap);

这是我在MainActivity中检索位图并将其转换为可绘制

的方法
Bitmap bitmap = (Bitmap) b.getParcelable("DRAWABLE");
Drawable drawable = new BitmapDrawable(getResources(),bitmap);