我正在使用微调器并且我的微调器工作正常,但旋转器的项目不能保存,例如当我为我的文本选择font2时我的文本字体是更改而我的更改保存为下一个但在spinner show font1关闭我的程序后 我的代码是: 在此代码中,我将项目添加到微调器
package com.testfont.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
public class font extends Activity {
public static String font="tahoma.ttf";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.font);
final List<String> list = new ArrayList<String>();
String item1 = "font1";
String item2 = "font2";
//String item3 = "فونت سه";
list.add(item1);
list.add(item2);
//list.add(item3);
Arrayadapt ad = new Arrayadapt(getApplicationContext(), 0, list);
Spinner sp = (Spinner)findViewById(R.id.spinner1);
sp.setAdapter(ad);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View selectedItemView,
int position, long id) {
// TODO Auto-generated method stub
//boolean selected = false;
if(position == 0){
font="tahoma.ttf";
}else if(position == 1){
font="QuranTaha.ttf";
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
// TODO Auto-generated method stub
public String getFont() {
// TODO Auto-generated method stub
return font;
}
}
在这段代码中我添加到spinner
package com.testfont.test;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class Arrayadapt extends ArrayAdapter{
Typeface tf;
List<String> _list;
Context context;
LayoutInflater mInflater;
public Arrayadapt(Context _context, int _resource,
List<String> _items) {
super(_context, _resource, _items);
// TODO Auto-generated constructor stub
this.context = _context;
this.tf = Typeface.createFromAsset(_context.getAssets(),"font/Yekan.ttf");
this._list = _items;
this.mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = mInflater;
View row = inflater.inflate(R.layout.tspinner, parent,
false);
TextView v = (TextView) row.findViewById(R.id.textView1);
v.setTypeface(tf);
v.setText(Farsi.Convert(_list.get(position)));
return row;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = mInflater;
View row = inflater.inflate(R.layout.tspinner, parent,
false);
TextView v = (TextView) row.findViewById(R.id.textView1);
v.setTypeface(tf);
v.setText(Farsi.Convert(_list.get(position)));
return row;
}
}
答案 0 :(得分:0)
A)。如果您在微调器中的项目是静态的。您可以使用SharedPreferences http://developer.android.com/training/basics/data-storage/shared-preferences.html
例如,您可以在onPause函数中保存微调器位置并在onResume函数中加载微调器位置。
B)。如果你的物品是dinamic。您可以使用sqlite存储项目和微调器位置。 http://developer.android.com/training/basics/data-storage/databases.html