-1

我正在使用微调器,我的微调器运行良好,但微调器项目没有保存,例如当我为我的文本选择 font2 时,我的文本字体已更改,我的更改已保存为下一个但在关闭我的程序后微调器显示 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;
}
}

在这段代码中,我添加到微调器

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;
    }
}
4

1 回答 1

0

一个)。如果您在微调器中的项目是静态的。您可以使用 SharedPreferences http://developer.android.com/training/basics/data-storage/shared-preferences.html

例如,您可以在 onPause 函数中保存微调器位置,在 onResume 函数中加载微调器位置。

乙)。如果您的物品是动态的。您可以使用 sqlite 来存储项目和微调器位置。 http://developer.android.com/training/basics/data-storage/databases.html

于 2013-09-01T11:25:48.350 回答