Java 在Eclipse中,如何在字符串资源中指定的字符串旁边打印存储在共享首选项中的值?
我希望屏幕上显示以下内容: Stringfrom字符串资源:值来自共享首选项 Stringfrom字符串资源:值来自共享首选项 等等 注释后编辑的my.java文件:Java 在Eclipse中,如何在字符串资源中指定的字符串旁边打印存储在共享首选项中的值?,java,android,xml,Java,Android,Xml,我希望屏幕上显示以下内容: Stringfrom字符串资源:值来自共享首选项 Stringfrom字符串资源:值来自共享首选项 等等 注释后编辑的my.java文件: import android.app.ActionBar.LayoutParams; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPr
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ActivityResults5 extends Activity {
Button button, submitButton;
SharedPreferences sharedPreferences= getSharedPreferences("MyData", Context.MODE_PRIVATE);
String oResult = getResources().getString(R.string.resultsO) + sharedPreferences.getString("facet1", "Default");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout activityResults5= new LinearLayout(this);
TextView tvO=new TextView(this);
Button b=new Button(this);
LayoutParams dimensions= new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
activityResults5.setLayoutParams(dimensions);
LayoutParams viewDimensions= new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tvO.setLayoutParams(viewDimensions);
b.setLayoutParams(viewDimensions);
activityResults5.setOrientation(LinearLayout.VERTICAL);
tvO.setText(oResult);
b.setText("Button here");
activityResults5.addView(tvO);
activityResults5.addView(b);
setContentView(activityResults5);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
submitButton = (Button) findViewById(R.id.submitButton);
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, ActivityResults12.class);
startActivity(intent);
}
});
}
}
试试这个
submitButton .setText(getResources().getString(R.string.submit+<Your shared preference value>));
这是将存储在共享首选项中的字符串打印到资源中字符串旁边的正确方法:
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ActivityResults5 extends Activity {
Button button, submitButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPreferences= getSharedPreferences("MyData", Context.MODE_PRIVATE);
String oResult = getResources().getString(R.string.resultsO) + sharedPreferences.getString("facet1", "Default");
LinearLayout activityResults5= new LinearLayout(this);
TextView tvO=new TextView(this);
Button b=new Button(this);
LayoutParams dimensions= new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
activityResults5.setLayoutParams(dimensions);
LayoutParams viewDimensions= new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tvO.setLayoutParams(viewDimensions);
b.setLayoutParams(viewDimensions);
activityResults5.setOrientation(LinearLayout.VERTICAL);
tvO.setText(oResult);
b.setText("Button here");
activityResults5.addView(tvO);
activityResults5.addView(b);
setContentView(activityResults5);
}
}
在共享首选项中保存值的位置?只需连接两个字符串:String newText=getResources.getStringR.String.submit+sharedPref.getStringkey_value;@ρцσѕρєцK:我正在保存一个名为MyData的XML文件。我没有在屏幕上显示我请求用户输入的内容,这些内容保存到共享首选项中。@SweetWisherシ: 我可能把我的提交按钮弄糊涂了。因为对我的问题的任何回答都不会包括它。该按钮仅用于将用户带到下一个活动屏幕。也就是说,我希望连接的字符串在用户到达此屏幕时显示在屏幕上,无需单击按钮。您可以将此代码放入onCreate中。我可能将您与我的提交按钮混淆了。因为对我的问题的任何回答都不会包括它。该按钮仅用于将用户带到下一个活动屏幕。也就是说,我希望连接的字符串在用户到达此屏幕时显示在屏幕上,此屏幕上无需单击任何按钮。getResources.getStringR.string.submit+将为您提供字符串,现在以您希望使用的任何方式使用它