如何在Activity中设置数据并获取java类

时间:2016-09-08 10:53:15

标签: android class android-studio android-activity static

更新:

我已经构建了一个运行良好的图像裁剪应用程序,但现在我想将裁剪后的图像名称保存为文本框值。

简而言之,我试图在object中设置textbox值并在java Class中获取对象值。我尝试了几种技术,最近我试图通过使用界面技术设置数据,图像仅保存为“.jpg”。

我很想知道我要去哪儿wronk?

以下是我尝试的代码。

MainActivity

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

接口

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

6 个答案:

答案 0 :(得分:2)

将formid设置为EditText值并获取CropHelper类中的返回值。

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

现在,在要调用formid值的类中创建Activity的对象。

MainActivity my_objec= new MainActivity();
    String id= my_objec.formid;
    String name = String.format(""+id+".jpg",System.currentTimeMillis());

这就是你需要做的一切。

答案 1 :(得分:1)

在您的课程中实现此功能,并在界面

中返回您的值
public interface onTaskComplete {
      void onComplete(String response);
}

答案 2 :(得分:0)

通常我所做的是创建不同的类,该类保存/保存可以在app中的不同类中使用的所有数据和值。

答案 3 :(得分:0)

例如:

// your activity
private CropHelper cropHelper;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CropHelper.REQUEST_CROP) {
        cropHelper.onReceivedImageData(data.get...)
    }
}

public interface DataCallBack {
    public void OnReceivedImageData(Data data);
}

....

// your crop helper
public class CropHelper implements YourActivity.DataCallBack {

    @Override
    public void OnReceivedImageData(Data data) {
        // doing anything with data
    }
}

答案 4 :(得分:0)

通过构造函数..,

中的参数传递数据

例如..在您的类中创建构造函数。

public class CropHelper {

    private Context context;
    private String msg;

    public CropHelper(Context context, String msg) {
        this.context = context;
        this.msg = msg;

        if (msg != null) {
            showMsg(msg);
        }
    }

    //Replace with your logic
    void showMsg(String msg) {
        //Perform your operation
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
}

然后通过创建该类的实例从任何Activity中简单地调用它。

像..

new CropHelper(this, "Hello from Activity");

答案 5 :(得分:0)

最佳方法是使用界面尝试如下: 创建界面

   public interface MyListener {
    // you can define any parameter as per your requirement
    public void callback(View view, String result);
}

public class MyActivity extends Activity implements MyListener {
   @override        
   public void onCreate(){
        MyButton m = new MyButton(this);
   }

    // method invoke when mybutton will click
    @override
    public void callback(View view, String result) {   
        // do your stuff here
    }
}

public class MyButton {
    MyListener ml;

    // constructor
    MyButton(MyListener ml) {
        this.ml = ml;
    }

    public void MyLogicToIntimateOthere() {
        ml.callback(this, "success");
    }
}

更多信息请访问此链接:

  

Using Interface