2011-05-04 5 views
1

환경 설정에 문제가 있습니다. 나는이 간단한 프로젝트를 만들어 : start.java :기본 레이아웃의 Android 환경 설정

package example.ex; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 

public class start extends Activity { 
    Intent intent; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
    public void preferences (View view){ 
     intent = new Intent(getApplicationContext(), Preferences.class); 
     startActivity(intent);  
    } 
} 

Preferences.java :

package example.ex; 

import android.os.Bundle; 
import android.preference.PreferenceActivity; 

public class Preferences extends PreferenceActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 
    } 
} 

주요 활동이 환경에 우리가 (버튼을 통해) 이동되는 시작을,입니다. 환경 설정에는 환경 설정 (팝업이있는 목록)이 표시됩니다.

레이아웃 : main.xml에 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
    <Button 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="preferences" 
    android:onClick="preferences" /> 
</LinearLayout> 

preferenze.xml : 내가 넣어 값

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 
    <TableRow> 
     <TextView 
       android:text="List Selection:" 
       android:paddingRight="5px" 
     /> 
     <TextView android:id="@+id/list" 
     /> 
    </TableRow> 
</TableLayout> 

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string-array name="country"> 
     <item>U.S. (United States of America)</item> 
     <item>Italia (Italy)</item> 
    </string-array> 
    <string-array name="codice_paese"> 
     <item>US</item> 
     <item>IT</item> 
    </string-array> 
</resources> 

을 arrays.xml 마지막으로, 나는 환경을 만들었습니다. xml (res에있는 폴더 xml) :

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
    <PreferenceCategory android:title="Simple Preferences"> 
     <ListPreference android:key="list" android:title="Selection Dialog" 
      android:summary="Click to pop up a list to choose from" 
      android:entries="@array/country" android:entryValues="@array/codice_paese" 
      android:dialogTitle="Choose a country" /> 
    </PreferenceCategory> 
</PreferenceScreen> 

다음 : 메인이 있고 환경 설정 페이지로 이동합니다.

내가 (대신 버튼의) 주요 환경에 직접 넣어 하나 개의 활동

를 사용하고 싶습니다하지만 난 당신이 어떤 제안을하시기 바랍니다 .. 어떤 방법으로 그것을 할 수없는 는? 덕분에 !

답변

0

대신 그 라인을 따라

package com.example.spinnerpref; 

import android.app.Activity; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.AdapterView.OnItemSelectedListener; 

public class SpinnerPref extends Activity 
{ 
int countrypos; 
public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
    countrypos = prefs.getInt("countrypos", 0); 
    setContentView(R.layout.main); 

    Spinner countryspinner = (Spinner) findViewById(R.id.country); 
    ArrayAdapter<CharSequence> countryadapater = ArrayAdapter.createFromResource(
      this, R.array.country, android.R.layout.simple_spinner_item); 
    countryadapater.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    countryspinner.setAdapter(countryadapater); 
    countryspinner.setOnItemSelectedListener(new countrySelector()); 

    class countrySelector implements OnItemSelectedListener 
    { 
     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) 
      { 
      countrypos = pos; 
      } 
     public void onNothingSelected(AdapterView<?> parent) {} 
    } 

    PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) 
     .edit().putInt("countrypos", countrypos).commit(); 
    } 
} 

뭔가 깔끔한 당신이 무엇을하고 있는지에 대한 있어야한다 SharedPreferencesSpinner를 사용하여이 같은 것을보십시오.

+0

따라서 main.xml 및 strings.xml을 적절하게 업데이트하십시오. 나는 그것으로 아주 thourough가 아니었다 그러나 나가 didnt는 당신이 그것의 무엇이든에 관하여 질문이있는 경우에 당신이 그것을 또는 더 이상 필요로하지 않을지도 모르다는 것을 기회에 이것에 시간의 톤을 소요하고 싶다 그래서 저에게 알고있다. 아프다. –

관련 문제