문자열 데이터에 putExtra () 및 getExtra ()를 사용하는 방법
누군가 정확히 사용 방법 getExtra()
과 putExtra()
의도 를 알려주십시오 . 실제로 str과 같은 문자열 변수가 있는데 일부 문자열 데이터를 저장합니다. 이제이 데이터를 한 활동에서 다른 활동으로 보내려고합니다.
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String keyIdentifer = null;
i.putExtra(strName, keyIdentifer );
그런 다음 SecondScreen.java에서
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
TextView userName = (TextView)findViewById(R.id.userName);
Bundle bundle = getIntent().getExtras();
if(bundle.getString("strName")!= null)
{
//TODO here get the string stored in the string variable and do
// setText() on userName
}
}
나는 그것이 매우 기본적인 질문이라는 것을 알고 있지만 불행히도 나는 여기에 붙어 있습니다. 도와주세요.
감사,
편집 : 여기에서 한 화면에서 다른 화면으로 전달하려고하는 문자열은 동적입니다. 즉, 사용자 유형에 관계없이 문자열을 얻는 editText가 있습니다. 그런 다음의 도움으로 myEditText.getText().toString()
. 입력 된 값을 문자열로 가져 오고이 데이터를 전달해야합니다.
이것을 사용하여 파일을 "넣습니다"...
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String strName = null;
i.putExtra("STRING_I_NEED", strName);
그런 다음 값을 검색하려면 다음과 같이 시도하십시오.
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}
첫 번째 Screen.java
text=(TextView)findViewById(R.id.tv1);
edit=(EditText)findViewById(R.id.edit);
button=(Button)findViewById(R.id.bt1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String s=edit.getText().toString();
Intent ii=new Intent(MainActivity.this, newclass.class);
ii.putExtra("name", s);
startActivity(ii);
}
});
두 번째 Screen.java
public class newclass extends Activity
{
private TextView Textv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent);
Textv = (TextView)findViewById(R.id.tv2);
Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
String j =(String) b.get("name");
Textv.setText(j);
}
}
}
최선의 방법 ...
SendActActivity
Intent intent = new Intent(SendingActivity.this, RecievingActivity.class);
intent.putExtra("keyName", value); // pass your values and retrieve them in the other Activity using keyName
startActivity(intent);
활동 받기
Bundle extras = intent.getExtras();
if(extras != null)
String data = extras.getString("keyName"); // retrieve the data using keyName
/// 가장 짧은 데이터 수신 방법 ..
String data = getIntent().getExtras().getString("keyName","defaultKey");
// 이것은 api 12가 필요합니다. keyName이 null이면 defaultkey
as 데이터 를 사용하십시오 .
이것은 내가 사용했던 것입니다, 다행히도 그것은 누군가를 돕습니다.. 간단하고 정서적입니다.
데이터를 보내다
intent = new Intent(getActivity(), CheckinActivity.class);
intent.putExtra("mealID", meal.Meald);
startActivity(intent);
데이터를 얻다
int mealId;
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null){
mealId = bundle.getInt("mealID");
}
건배!
그것은 구현하기가 매우 쉽습니다 intent
그것은 하나 개의 활동에서 다른 활동으로 이동 이동합니다 .. 안드로이드에, 우리는 두 가지 방법이 putExtra();
그리고 getExtra();
지금 나는 당신에게 예를 도시하고있다 ..
Intent intent = new Intent(activity_registration.this, activity_Login.class);
intent.putExtra("AnyKeyName", Email.getText().toString()); // pass your values and retrieve them in the other Activity using AnyKeyName
startActivity(intent);
이제 AnyKeyName
매개 변수 에서 값을 가져와야합니다. 아래 언급 된 코드는이 작업을 수행하는 데 도움이됩니다.
String data = getIntent().getExtras().getString("AnyKeyName");
textview.setText(data);
Intent
어디에서나 필요한 값을 쉽게 수신 할 수 있습니다 .
작은 부록 : 키에 대한 자신의 이름을 만들 필요가 없으며 안드로이드는 f.ex를 제공합니다. Intent.EXTRA_TEXT
. 허용 된 답변 수정 :
Intent i = new Intent(FirstScreen.this, SecondScreen.class); String strName = null; i.putExtra(Intent.EXTRA_TEXT, strName);
그런 다음 값을 검색하려면 다음과 같이 시도하십시오.
String newString; Bundle extras = getIntent().getExtras(); if(extras == null) { newString= null; } else { newString= extras.getString(Intent.EXTRA_TEXT); }
Intent intent = new Intent(view.getContext(), ApplicationActivity.class);
intent.putExtra("int", intValue);
intent.putExtra("Serializable", object);
intent.putExtra("String", stringValue);
intent.putExtra("parcelable", parObject);
startActivity(intent);
ApplicationActivity
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null){
int mealId = bundle.getInt("int");
Object object = bundle.getSerializable("Serializable");
String string = bundle.getString("String");
T string = <T>bundle.getString("parcelable");
}
인 텐트 클래스 에서 업데이트
hasExtra()
의도에 키에 대한 데이터가 있는지 확인하는 데 사용 합니다.- 지금
getStringExtra()
바로 사용할 수 있습니다 .
데이터 전달
intent.putExtra(PutExtraConstants.USER_NAME, "user");
데이터 가져 오기
String userName;
if (getIntent().hasExtra(PutExtraConstants.USER_NAME)) {
userName = getIntent().getStringExtra(PutExtraConstants.USER_NAME);
}
모범 사례로 항상 상수를 키에 넣으십시오.
public interface PutExtraConstants {
String USER_NAME = "USER_NAME";
}
더 간단한
발신자 측
Intent i = new Intent(SourceActiviti.this,TargetActivity.class);
i.putExtra("id","string data");
startActivity(i)
수신기 측
Intent i = new Intent(SourceActiviti.this,TargetActivity.class);
String strData = i.getStringExtra("id");
기능을 넣어
etname=(EditText)findViewById(R.id.Name);
tvname=(TextView)findViewById(R.id.tvName);
b1= (ImageButton) findViewById(R.id.Submit);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String s=etname.getText().toString();
Intent ii=new Intent(getApplicationContext(), MainActivity2.class);
ii.putExtra("name", s);
Toast.makeText(getApplicationContext(),"Page 222", Toast.LENGTH_LONG).show();
startActivity(ii);
}
});
getfunction
public class MainActivity2 extends Activity {
TextView tvname;
EditText etname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
tvname = (TextView)findViewById(R.id.tvName);
etname=(EditText)findViewById(R.id.Name);
Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
String j2 =(String) b.get("name");
etname.setText(j2);
Toast.makeText(getApplicationContext(),"ok",Toast.LENGTH_LONG).show();
}
}
데이터 푸시
import android.content.Intent;
...
Intent intent =
new Intent(
this,
MyActivity.class );
intent.putExtra( "paramName", "paramValue" );
startActivity( intent );
위 코드는 main 안에있을 수 있습니다 activity
. " MyActivity.class
"는 Activity
우리가 발사하고자 하는 두 번째입니다 . AndroidManifest.xml
파일에 명시 적으로 포함되어야 합니다.
<activity android:name=".MyActivity" />
데이터 풀
import android.os.Bundle;
...
Bundle extras = getIntent().getExtras();
if (extras != null)
{
String myParam = extras.getString("paramName");
}
else
{
//..oops!
}
이 예제에서 위의 코드는 MyActivity.java
파일 안에 있습니다 .
잡았다
이 방법은 통과 할 수 있습니다 strings
. 그럼 당신이를 통과해야 가정 해 봅시다 ArrayList
당신에 ListActivity
; 가능한 해결 방법은 쉼표로 구분 된 문자열을 전달한 다음 반대쪽으로 나누는 것입니다.
대체 솔루션
사용하다 SharedPreferences
첫 번째 활동에서 단순
EditText name= (EditText) findViewById(R.id.editTextName);
Button button= (Button) findViewById(R.id.buttonGo);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Main2Activity.class);
i.putExtra("name",name.getText().toString());
startActivity(i);
}
});
두 번째 활동에서
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView t = (TextView) findViewById(R.id.textView);
Bundle bundle=getIntent().getExtras();
String s=bundle.getString("name");
t.setText(s);
}
원하는 경우 if / else 조건을 추가 할 수 있습니다.
의도 객체에 문자열 넣기
Intent intent = new Intent(FirstActivity.this,NextAcitivity.class);
intent.putExtra("key",your_String);
StartActivity(intent);
onCreate 메소드의 NextAcitvity get String
String my_string=getIntent().getStringExtra("key");
그것은 쉽고 짧은 방법입니다
끈을 먼저 놓다
Intent secondIntent = new Intent(this, typeof(SecondActivity));
secondIntent.PutExtra("message", "Greetings from MainActivity");
그 후 그것을 검색
var message = this.Intent.GetStringExtra("message");
그게 다야 ;)
보내다
startActivity(new Intent(First.this, Secend.class).putExtra("key",edit.getText.tostring));
가져 오기
String myData = getIntent.getStringExtra("key");
정적 변수를 사용하여 편집 텍스트의 문자열을 저장 한 다음 다른 클래스에서 해당 변수를 사용할 수 있습니다. 이것이 문제를 해결할 수 있기를 바랍니다.
FirstScreen.java에서
Intent intent = new Intent(FirstScreen.this, SecondScreen.class);
String keyIdentifier = null;
intent.putExtra(strName, keyIdentifier);
SecondScreen.java에서
String keyIdentifier;
if (savedInstanceState != null)
keyIdentifier= (String) savedInstanceState.getSerializable(strName);
else
keyIdentifier = getIntent().getExtras().getString(strName);
참고 URL : https://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data
'development' 카테고리의 다른 글
파이썬리스트는 요소가 삽입 된 순서대로 유지되도록 보장합니까? (0) | 2020.03.17 |
---|---|
Mac OS X 10.8 / Xcode 4.4에서 gcc 사용 / 설치 방법 (0) | 2020.03.17 |
파일 출력으로 디렉토리 자동 생성 (0) | 2020.03.17 |
미적으로 유쾌한 색상 팔레트를 임의로 생성하는 알고리즘 (0) | 2020.03.17 |
Android“…”를 줄임표 문자로 바꿉니다. (0) | 2020.03.17 |