[英]In Eclipse, How do I print a value stored in shared preferences next to a string specified in string resources?
我希望以下内容出现在屏幕上:
字符串(来自字符串资源):值(来自共享首选项)
字符串(来自字符串资源):值(来自共享首选项)
等等
我的.java文件(在注释后编辑):
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.