2010-11-17 57 views
0

我已經搜索和搜索,我只是不能讓這段代碼工作。我有一個main.xml佈局和一個setting.xml.I有一些值,我希望Settings.class更改在我的主要應用程序class.Three字符串是確切的。傳遞意圖的值

我在主要的應用程序類

settings.setOnClickListener(new OnClickListener(){ 

    @Override 
    public void onClick(View v) { 
    Intent intent = new Intent(v.getContext(), Settings.class); 
    startActivityForResult(intent, 0); 


    } 
    }); 

//Then a function 
@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){ 
     super.onActivityResult(requestCode, resultCode, intent); 
     Bundle extras = intent.getExtras(); 
     String value = extras.getString("myKey"); 
     if(value!=null){ 
     Log.d("hmmm",value); 
     } 
    } 
} 

嘗試這種簡單的測試代碼在我settings.class我有以下

returnHome.setOnClickListener(new OnClickListener(){ 

    @Override 
    public void onClick(View v) { 
    Intent intent = new Intent(); 
    intent.putExtra("myKey", "YEAH"); 
       setResult(RESULT_OK, intent); 
       finish(); 


    } 

     }); 

回到它是沒有得到記錄主要的應用程序類。

就像我說過的,我在主類中有三個字符串,我希望設置類可以更改併發回。 非常感謝任何幫助

+0

快速瀏覽後,你的代碼看起來是正確的 - 出於利益考慮,在你的清單文件,你是否爲你的主要活動或設置活動設置了一個特定的'android:launchMode'? – Scoobler 2010-11-17 11:24:23

回答

1

我已經成功地使用了Notepad tutorial中使用的技術,其中信息被放置在Bundle然後添加到意圖。請參閱步驟10:

Bundle bundle = new Bundle(); 

bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString()); 
bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString()); 
if (mRowId != null) { 
    bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId); 
} 

Intent mIntent = new Intent(); 
mIntent.putExtras(bundle); 
setResult(RESULT_OK, mIntent); 
finish(); 
0

你的代碼工作對我很好,這裏的一大堆

Main.java

package com.test; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class Main extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button settings = (Button) findViewById(R.id.settings); 
     settings.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(v.getContext(), Settings.class); 
      startActivityForResult(intent, 0); 
     } 
     }); 
    } 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, 
     Intent intent) { 
     super.onActivityResult(requestCode, resultCode, intent); 
     Bundle extras = intent.getExtras(); 
     String value = extras.getString("myKey"); 
     if (value != null) { 
     Log.d("hmmm", value); 
     } 
    } 
} 

Settings.java

package com.test; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class Settings extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.settings); 
     Button returnHome = (Button) findViewById(R.id.returnHome); 
     returnHome.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(); 
      intent.putExtra("myKey", "YEAH"); 
      setResult(RESULT_OK, intent); 
      finish(); 
     } 
     }); 
    } 
} 

的main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <Button 
      android:text="Settings" 
      android:id="@+id/settings" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"></Button> 
</LinearLayout> 

的settings.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <Button 
      android:text="Return home" 
      android:id="@+id/returnHome" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"></Button> 
</LinearLayout> 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.test" 
     android:versionCode="1" 
     android:versionName="1.0"> 
     <application 
      android:icon="@drawable/icon" 
      android:label="@string/app_name"> 
      <activity 
        android:name=".Main" 
        android:label="@string/app_name"> 
        <intent-filter> 
         <action 
           android:name="android.intent.action.MAIN" /> 
         <category 
           android:name="android.intent.category.LAUNCHER" /> 
        </intent-filter> 
      </activity> 
      <activity 
        android:name="Settings" 
        android:label="Settings" /> 
     </application> 
</manifest> 

而且logcat中走了出來:

11-17 12:21:46.717: DEBUG/hmmm(258): YEAH 
+0

我知道它使用捆綁工作,奇怪的仍然不會使用above.Thanks花時間來打字。 – James 2010-11-17 12:44:14