gpt4 book ai didi

android - 如何从另一项 Activity 中获取值(value)?

转载 作者:行者123 更新时间:2023-11-29 17:49:17 27 4
gpt4 key购买 nike

我想在 Resigter 模型中设置值。我想分四步创建一个注册 Activity 。我想知道如何在注册模型中设置值。而且我必须从任何地方获取该值。

这是我的代码 所有值都放在一个 Activity 中。我想做四步

public void UploadData(final String link) {

Response = "";

try {

HttpResponse response;

Log.d("pre_link", "pre_link = " + link);

final HttpClient httpclient = new DefaultHttpClient();

final HttpPost httppost = new HttpPost(link);

/*httppost.addHeader("Authorization", "Basic "
+ Base64.encodeToString(("username" + ":"
+ "password").getBytes(), Base64.NO_WRAP));*/

MultipartEntity mpEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);

String FullName = fullName.getText().toString();
String UserName = userName.getText().toString();
String DateOfBirth = dob.getText().toString();
String Age = age.getText().toString();

String Sex = gender.getText().toString();
String InterestedIn = interestIn.getText().toString();
String ToMeet = "both";//toMeet.getText().toString();

String Email = email.getText().toString();
String Password = pwd.getText().toString();
String Lat = String.valueOf(latitude);
String Long = String.valueOf(longitude);

mpEntity.addPart("fullName", new StringBody(FullName));
mpEntity.addPart("userName", new StringBody(UserName));
mpEntity.addPart("dob", new StringBody(DateOfBirth));
mpEntity.addPart("age", new StringBody(Age));
mpEntity.addPart("gender", new StringBody(Sex));
mpEntity.addPart("interestIn", new StringBody(InterestedIn));
mpEntity.addPart("toMeet", new StringBody(ToMeet));
mpEntity.addPart("email", new StringBody(Email));
mpEntity.addPart("pwd", new StringBody(Password));

mpEntity.addPart("latitude", new StringBody(Lat));
mpEntity.addPart("longitude", new StringBody(Long));

if (bab1 != null) {
mpEntity.addPart("uploaded_file", bab1);
}

httppost.setEntity(mpEntity);

createCancelProgressDialog("Uploading Image", "Please wait...", "Cancel");

new Thread() {
public void run() {
try {
HttpResponse response;
Message msg = new Message();

msg.what = 1;

try {
response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Response = EntityUtils.toString(resEntity)
.trim();

Log.d("Response", "Response = " + Response);

Message msg2 = new Message();
msg2.what = 1;
UpdateHandler.sendMessage(msg2);

}
if (resEntity != null) {
resEntity.consumeContent();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

} catch (Exception e) {
Log.e("tag", e.getMessage());
}

}
}.start();

} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

public Handler UpdateHandler = new Handler() {
public void handleMessage(Message msg) {

switch (msg.what) {
case 1:
try {

cancelDialog.dismiss();
cancelDialog.hide();

Log.d("Response", "Response = " + Response);

Toast.makeText(SignUp.this,"you are Success", Toast.LENGTH_SHORT).show();
RegisterModel register =new RegisterModel();
//register.setfullName();

Intent i = new Intent(getApplicationContext(),SignupSuccessfully.class);

// i.putExtra("pwd",pwsd);
startActivity(i);
finish();
//flag=1;




//String read_data = ReadDataFromAppCache(MainActivity.this, "file_name");
//StoreDataToAppCache(MainActivity.this, "file data", "file_name");
} catch (Exception e) {
// TODO: handle exception
}
super.handleMessage(msg);
}
}
};

ProgressDialog cancelDialog = null;

private void createCancelProgressDialog(String title, String message,
String buttonText) {
cancelDialog = new ProgressDialog(SignUp.this);
cancelDialog.setTitle(title);
cancelDialog.setMessage(message);
cancelDialog.setCanceledOnTouchOutside(false);
// cancelDialog2.setIcon(R.drawable.icon);

/*cancelDialog.setButton(buttonText,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
cancelDialog.dismiss();
cancelDialog.hide();
return;
}
});*/
cancelDialog.show();
}

public Bitmap setBitmap(String _path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
options.inPurgeable = true;
//options.inJustDecodeBounds = true;
Bitmap bitmap = null;

ExifInterface exif;
try {
bitmap = BitmapFactory.decodeFile(selectedImagePath, options);

exif = new ExifInterface(_path);
int exifOrientation = exif
.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);

int rotate = 0;

switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;

case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;

case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
}

//Log.d("image_rotation", "image_rotation = " + rotate);

if (rotate != 0) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();

// Setting pre rotate
Matrix mtx = new Matrix();
mtx.preRotate(rotate);

// Rotating Bitmap & convert to ARGB_8888, required by tess
bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return bitmap;
}

public String ReadDataFromAppCache(Context context, String file_name) {

String output = "";
Log.d("file name", "file name = " + file_name);
try {
int ch;
File f = new File(context.getFilesDir() + "/" + file_name);
//Log.d("file path", "" + f.getAbsolutePath());

StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;

try {
fin = new FileInputStream(f);

while ((ch = fin.read()) != -1)
strContent.append((char) ch);

fin.close();
} catch (FileNotFoundException e) {
//Log.d("File " + f.getAbsolutePath(), " could not be found on filesystem");
output = "null";
return output;
} catch (IOException ioe) {
//Log.d("Exception while reading the file", "Exception while reading the file" + ioe);
}

try {
output = URLDecoder.decode(strContent.toString(), "UTF-8");
} catch (UnsupportedEncodingException uee) { }

//Log.d("This is xml", "This is xml" + strContent);

//output = strContent.toString();

} catch (Exception e) {
// TODO: handle exception
}
return output;
}

public void StoreDataToAppCache(Context con, String fileData, String file_name) {
try {
String encodedValue = "";
try {
encodedValue = URLEncoder.encode(fileData, "UTF-8");
} catch (UnsupportedEncodingException uee) {
}

//encodedValue = sBody;
//Log.d("store text", "store_text = " + encodedValue);
File f = new File(con.getFilesDir() + "/" + file_name);

FileWriter writer = new FileWriter(f);
writer.append(encodedValue);
writer.flush();
writer.close();
Log.d("save complete", "save complete");
} catch (IOException e) {
e.printStackTrace();
}
}

你能告诉我如何在寄存器中设置这些值吗?然后我将创建四步注册。

最佳答案

您可以使用以下方法之一执行此操作

  1. 使用共享首选项保存您的数据,您可以从应用程序中的任何位置访问它(推荐)。
  2. 如果你有更多的数据要存储,我建议使用数据库。
  3. 您可以使用 intent.putExtra() 方法将值从一个 Activity 传递到另一个 Activity 。但是您必须为所有新 Activity 执行此操作
  4. 另一种简单的方法是使您的变量成为公共(public)静态变量,并使用静态引用从项目中的任何位置访问数据。 (不推荐)。

希望对您有所帮助。 :)

关于android - 如何从另一项 Activity 中获取值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24237045/

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