如何在Activity中设置数据并获取java类 [英] How to set data in Activity and get in java class

查看:194
本文介绍了如何在Activity中设置数据并获取java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已建立图片裁剪应用程式运行正​​常,但现在我要将裁剪后的图片名称保存为文本框值。

I have build a image cropping app its running fine, but now I want to save cropped image name as textbox value.

总之,我试图在对象中设置textbox值,并在java类中获取对象值。我尝试了几种技巧,最近我试图获得,使用接口技术设置数据,图像只保存为.jpg。

In short I am trying to set textbox value in object and get object value in java Class. I have tried several techniques, recently I am trying to get,set data by using interface technique and the image is saved as ".jpg"only.

以下是我尝试过的代码。

Following is the code I have tried.

public class TestActivity extends AppCompatActivity implements CropHandler, View.OnClickListener {

    public static final String TAG = "TestActivity";

    ImageView mImageView;
    EditText formnumber;

    String formid;

    CropParams mCropParams;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        mCropParams = new CropParams(this);
        mImageView = (ImageView) findViewById(R.id.image);
        formnumber =(EditText)findViewById(R.id.FormNumber);


        findViewById(R.id.bt_crop_capture).setOnClickListener(this);
        findViewById(R.id.bt_crop_gallery).setOnClickListener(this);


    }

    @Override

    public void onClick(View v) {
        mCropParams.refreshUri();
        formid=formnumber.getText().toString();

//        Intent i = new Intent(TestActivity.this, CropHelper.class);
//        i.putExtra("Id",formid);

if(formid.matches(""))
{
    Toast.makeText(getApplicationContext(),"Please Enter Application Id",Toast.LENGTH_SHORT).show();
}
else
{
    switch (v.getId()) {
        case R.id.bt_crop_capture: {
            mCropParams.enable = true;
            mCropParams.compress = false;

            Intent intent = CropHelper.buildCameraIntent(mCropParams);

            startActivityForResult(intent, CropHelper.REQUEST_CAMERA);

        }
        break;
        case R.id.bt_crop_gallery: {
            mCropParams.enable = true;
            mCropParams.compress = false;
            Intent intent = CropHelper.buildGalleryIntent(mCropParams);
            startActivityForResult(intent, CropHelper.REQUEST_CROP);
        }
        break;

    }
    }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        CropHelper.handleResult(this, requestCode, resultCode, data);
        if (requestCode == 1) {
            Log.e(TAG, "");
        }
    }

@Override
public void onTaskComplete(String response) {
    onTaskComplete(this.formid);
}
}



CropHelper类


b $ b

CropHelper Class

    public class CropHelper {
    public static final String TAG = "CropHelper";
    /**
     * request code of Activities or Fragments
     * You will have to change the values of the request codes below if they conflict with your own.
     */
    public static final int REQUEST_CROP = 127;
    public static final int REQUEST_CAMERA = 128;
    public static final int REQUEST_PICK = 129;

    public static String AppId;

    public static final String CROP_CACHE_FOLDER = "PhotoCropper";

    public static Uri generateUri() {
        File cacheFolder = new File(Environment.getExternalStorageDirectory() + File.separator + CROP_CACHE_FOLDER);
        if (!cacheFolder.exists()) {
            try {
                boolean result = cacheFolder.mkdir();

                Log.d(TAG, "generateUri " + cacheFolder + " result: " + (result ? "succeeded" : "failed"));
            } catch (Exception e) {
                Log.e(TAG, "generateUri failed: " + cacheFolder, e);
            }
        }

//        String name = String.format("image-%d.jpg", System.currentTimeMillis());
        String name = String.format(AppId.toString()+".jpg",System.currentTimeMillis());
        return Uri
                .fromFile(cacheFolder)
                .buildUpon()
                .appendPath(name)
                .build();
    }

@Override
public void onTaskComplete(String response) {
    AppId=response;
}
}



接口



Interface

    public interface CropHandler
{

    void onPhotoCropped(Uri uri);

    void onCompressed(Uri uri);

    void onTaskComplete(String response);

    void onCancel();

    void onFailed(String message);

    void handleIntent(Intent intent, int requestCode);

    CropParams getCropParams();
}


推荐答案

为了那个原因。只需将formid设置为EditText值,并将formid定义为static,并获取CropHelper类中的返回值。
以下是您需要在代码中进行的更改。

You dont need to use Interface for that. Just set formid to EditText value and define formid as static and get the return value in your CropHelper class. followings are the changes you need to do in your code.

public static String formid=null;
// set formid value
    formid=formnumber.getText().toString();





您的Activity的对象,并在genrateuri函数中获取formid值。

Class

Here you need to create an object of your Activity and get formid value in genrateuri function.

        public static Uri generateUri() {
        MainActivity my_objec= new MainActivity();
        String id= my_objec.formid;
        File cacheFolder = new File(Environment.getExternalStorageDirectory() + File.separator + CROP_CACHE_FOLDER);
        if (!cacheFolder.exists()) {
            try {
                boolean result = cacheFolder.mkdir();

                Log.d(TAG, "generateUri " + cacheFolder + " result: " + (result ? "succeeded" : "failed"));
            } catch (Exception e) {
                Log.e(TAG, "generateUri failed: " + cacheFolder, e);
            }
        }

//        String name = String.format("image-%d.jpg", System.currentTimeMillis());
        String name = String.format(""+id+".jpg",System.currentTimeMillis());
        return Uri
                .fromFile(cacheFolder)
                .buildUpon()
                .appendPath(name)
                .build();
    }

这篇关于如何在Activity中设置数据并获取java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆