我已搜索并搜索过,我无法使用此代码。我有一个main.xml布局和一个setting.xml。我有一些值,我想在我的主应用程序中更改Settings.class class.Three string确切。
我在我的主app类
中尝试过这个简单的测试代码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 :(得分: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();
答案 1 :(得分: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