我真的不知道它为什么不显示文字。
如果你看第2课,那么我将nimi2保存为共享偏好。但它没有出现在第1课。我做了一个小测试。我将名称2替换为“plaplapla”(注意“”),它显示为plaplapla。但我希望它能显示出任何类型的人作为他的名字。我是java的新手。我再次真的没有想法:(。有人可以帮忙吗?
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
public class MainStuff extends Activity {
TextView tere;
String nimi;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
tere = (TextView) findViewById(R.id.tvTere);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean nimiOlemas = preferences.getBoolean("nimionolemas", false);
nimi = preferences.getString("nimi2", "");
if (nimiOlemas == false){
startActivityForResult(new Intent(this, nimekysija.class), 0);
finish();
}
if (nimiOlemas == true){
tere.setText("Tere " + nimi);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// get data from shared preference here
super.onActivityResult(requestCode, resultCode, data);
}
}
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class nimekysija extends Activity {
EditText nimi;
SharedPreferences preferences;
String nimi2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.nimekysija);
preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
nimi = (EditText) findViewById(R.id.etNimekysija);
nimi2 = nimi.getText().toString();
Button kysOk = (Button) findViewById(R.id.bNimekysija);
kysOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences.Editor editor = preferences.edit();
editor.putString("nimi2", nimi2); // nime kirjutamine
editor.putBoolean("nimionolemas", true); // nimi on kirjutatud!
editor.commit();
startActivity(new Intent("viimane.voimalus.MAINSTUFF"));
finish();
}
});
}
}
and 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"
>
<TextView android:text="" android:id="@+id/tvTere" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="Palun sisestage oma nimi!" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"></TextView>
<EditText android:id="@+id/etNimekysija" android:layout_height="wrap_content" android:layout_width="fill_parent">
</EditText>
<Button android:text="OK!" android:id="@+id/bNimekysija" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onAddClick"></Button>
</LinearLayout>
答案 0 :(得分:1)
使用startActivity()的Instaed,使用名为startActivityForResult()
的方法。并且在类1中重写另一个名为onActivityResult()
的方法,在该方法中,您尝试获取String。现在,您正在尝试从sharedPreference
获取值,然后将其存储在class2中。
//编辑
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
public class MainStuff extends Activity {
TextView tere;
String nimi;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
tere = (TextView) findViewById(R.id.tvTere);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean nimiOlemas = preferences.getBoolean("nimionolemas", false);
if (nimiOlemas == false){
startActivity(new Intent("viimane.voimalus.NIMEKYSIJA"));
finish();
}
if (nimiOlemas == true){
nimi = preferences.getString("nimi2", "");
System.out.print("töötab!");
tere.setText("Tere " + nimi);
}
System.out.print("töötab2!");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// get data from shared preference here
super.onActivityResult(requestCode, resultCode, data);
}
}
编辑:我意外地编辑了它。现在修复了
答案 1 :(得分:0)
我为我的答案找到了解决方案。我删除了onActivityResult,并在第二类中做了一个小动作。将nimi2 = nimi.getText().toString();
移动到按钮上点击:)。
谢谢你ntc的帮助:)。我等不及你的回复所以我只搜索了很多搜索等等:P。