1

このラジオボタンにはまだ問題があります:(。ユーザーがテキストの色を選択できる設定ページを作成しようとしています.この設定アクティビティと他のすべてのアクティビティから選択すると、共有設定の変更もリッスンします. .

テキストを保存し、アクティビティ間の共有設定からテキストを取得する方法は知っていますが、int ? ラジオでチェックされた値が必要なため、このメソッドは他のアクティビティに渡され、テキストビューの色を設定します。

誰か私のためにコードをチェックしてもらえますか?

私はアンドロイドと一般的なプログラミングに非常に慣れていません。

public class Text_Colour extends Activity implements OnSharedPreferenceChangeListener {

    RadioButton rb1, rb2, rb3, rb4, rb5;
    TextView tv1, tv2;
    RadioGroup rg1;
    Button bt1;
    String red, yellow, green, blue;

    public static final String PREFS_NAME = "MyPrefsFile"; 


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.colours);

        loadPreferences();

        tv1 = (TextView)findViewById(R.id.textview1);
        tv2 = (TextView)findViewById(R.id.textview2);
        bt1 = (Button) findViewById(R.id.button1);
        rb1 = (RadioButton) findViewById(R.id.radioButton1);
        rb2 = (RadioButton) findViewById(R.id.radioButton2);
        rb3 = (RadioButton) findViewById(R.id.radioButton3);
        rb4 = (RadioButton) findViewById(R.id.radioButton4);
        rb5 = (RadioButton) findViewById(R.id.radioButton5);
        rg1 = (RadioGroup) findViewById(R.id.radiogroup1);

        bt1.setOnClickListener(Colour_change_b);

        //tv1.setOnSharedPreferenceChangeListener(this);

    }

    View.OnClickListener Colour_change_b = new View.OnClickListener() {

    public void onClick(View v) {

        if(v == bt1) {

             if (rb1.isChecked()== true) {
                 tv2.setTextColor(Color.RED);

                } 
                if (rb2.isChecked() == true) {
                    tv2.setTextColor(Color.YELLOW);

                } 
                if (rb3.isChecked() == true) {
                    tv2.setTextColor(Color.GREEN);

                } 
                if (rb4.isChecked() == true) {
                    tv2.setTextColor(Color.BLUE);

                } 
                if (rb5.isChecked() == true) {
                    rb5.getId();
                    tv2.setTextColor(Color.BLACK);

                } 
                else { 
                }
                SharedPreferences settings1 = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings1.edit();
                editor.putInt("colour", rb1.getId());
                editor.commit();
                finish();   
    }
    }
    };
    private void loadPreferences() {
        // TODO Auto-generated method stub
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        tv1.setTextColor(settings.getId("colour", ""));
        settings.registerOnSharedPreferenceChangeListener(Text_Colour.this);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {

        loadPreferences();
    }   
    }

あなたの時間に感謝します。

4

1 に答える 1

3

あなたのを使用してくださいRadioGroup。を使用setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener)して新しい色の値を即座に受け取ることができgetCheckedRadioButtonId()、チェック済みの radioButtonId を取得するために使用できます。

ただし、落とし穴があります。Idは、Android によってコンパイル時に生成されます。コンパイル間で一貫性が保たれる保証はありません。indexOfChild(View child)RadioGroup 内の RadioButton のインデックスを取得する場合に使用します。

値を取得するには、 a getInt()、次に a getChildAt(int index)、次に agetId()および aが必要check(int id)です。

于 2012-11-26T16:59:08.060 に答える