日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android intent.putextras,关于android:如何使用putExtra()和getExtra()来表示字符串数据

發布時間:2024/1/23 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android intent.putextras,关于android:如何使用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()的幫助下。 我將輸入的值作為字符串,然后我必須傳遞此數據。

i.putExtra(strName,keyIdentifer); 此語句具有strName變量,而bundle.getString("strName")具有"strName"字符串。 它的intent.putExtra(key,value)和intent.getExtras()。getString(key); 確保你在put和get中使用相同的鍵。

用它來"放"文件......

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");

}

是用于處理方向變化的"savedInstanceState ..."和"... getSerialiable"代碼? 如果不是,該代碼用于什么?

我使用Android 3.0.1,我不得不使用this.getActivity().getIntent().getExtras()。

首先是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);

}

}

}

最佳方法......

SendingActivity

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

RecievingActivity

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作為數據。

這就是我一直在使用的,它可以幫助某人......簡單而有效。

發送數據

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");

}

干杯!

我還是要時刻提醒自己,這是怎么做得好的。哈哈!

在Android中實現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設置接收值。

一個小附錄:你不必為密鑰創建自己的名字,android提供這些,f.ex。 Intent.EXTRA_TEXT。修改接受的答案:

Intent i = new Intent(FirstScreen.this, SecondScreen.class);

String strName = null;

i.putExtra(Intent.EXTRA_TEXT, strName);

Then, to retrieve the value try something like:

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");

}

在Intent類中更新。

使用hasExtra()檢查intent是否包含密鑰數據。

您可以直接使用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";

}

為什么PutExtraConstants是一個接口?

@Big_Chair因為PutExtraConstants類只包含常量(public,static,final)。 所以最好使用Constants接口。

更簡單

發件人方

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");

簡單,

在第一個活動中 -

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

}

You can add if/else conditions if you want.

推送數據

import android.content.Intent;

...

Intent intent =

new Intent(

this,

MyActivity.class );

intent.putExtra("paramName","paramValue" );

startActivity( intent );

上面的代碼可能在主activity中。"MyActivity.class"是我們要發布的第二個activity;它必須明確包含在AndroidManifest.xml文件中。

拉數據

import android.os.Bundle;

...

Bundle extras = getIntent().getExtras();

if (extras != null)

{

String myParam = extras.getString("paramName");

}

else

{

//..oops!

}

在此示例中,上面的代碼將位于MyActivity.java文件中。

陷阱

此方法只能傳遞strings。所以,假設您需要將ArrayList傳遞給ListActivity;一種可能的解決方法是傳遞逗號分隔字符串,然后在另一端分割它。

替代方案

使用SharedPreferences

如果我想從string.xml傳遞一個字符串怎么辦?

把功能

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

}

}

將字符串放入Intent對象中

Intent intent = new Intent(FirstActivity.this,NextAcitivity.class);

intent.putExtra("key",your_String);

StartActivity(intent);

onCreate方法中的NextAcitvity獲取String

String my_string=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);

歡迎來到SO! 請編輯您的答案并詳細說明解決問題的原因和方法。 有關更多指導,請參閱stackoverflow.com/help/how-to-answer

發送

startActivity(new Intent(First.this, Secend.class).putExtra("key",edit.getText.tostring));

得到

String myData = getIntent.getStringExtra("key");

您可以使用靜態變量來存儲edittext的字符串,然后在另一個類中使用該變量。希望這能解決你的問題

首先放字符串

Intent secondIntent = new Intent(this, typeof(SecondActivity));

secondIntent.PutExtra("message","Greetings from MainActivity");

之后檢索它

var message = this.Intent.GetStringExtra("message");

就這樣 ;)

總結

以上是生活随笔為你收集整理的android intent.putextras,关于android:如何使用putExtra()和getExtra()来表示字符串数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。