2016-05-20 68 views
-1

我有兩個微調器。一個用於width,另一個用於height,兩者都有兩個值。即如何根據寬度爲Android微調器選擇高度

width ==> 1280, 800 
height ==> 720, 600 





spin_width = (Spinner) findViewById(R.id.spin_width); 
spin_height = (Spinner) findViewById(R.id.spin_height); 
ArrayAdapter<CharSequence> adapter_width = ArrayAdapter.createFromResource(this,    
       R.array.width_array,R.layout.spinner_item);  
    adapter_width.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

spin_width.setAdapter(adapter_width); 
spin_width.setOnItemSelectedListener(new OnItemSelectedListener() { 

@Override 
public void onItemSelected(AdapterView<?> parent, View view, 
        int position, long id) { 
     // TODO Auto-generated method stub 

    String width = parent.getItemAtPosition(position).toString(); 
    mWidth = Integer.valueOf(width); 
    Log.d(TAG, "Spinner1"+mWidth); 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
      // TODO Auto-generated method stub 

      } 
    }); 

如果用戶從寬度框中選定1280中高度框720應該自動並且如果在寬度選擇800選擇,600應選擇在高度框。這個怎麼做 ?

感謝

+0

發佈您的代碼..它會更好地修改您的代碼.. –

+0

@RameshKumar:PL再次檢查上面的po ST。我添加了代碼 – Shiv

+0

您是否希望用戶能夠同時選擇1280的寬度和600的高度?如果他們只能選擇1280x720和800x600的配對,那麼使用這兩個選項就可以使用一個微調器就足夠了。 – panonski

回答

0

下面的函數,你需要什麼

private void selectValue(Spinner spinner, String value) { 


     for (int i = 0; i < spinner.getCount(); i++) { 

      if (spinner.getItemAtPosition(i).toString().equals(value)) { 
       spinner.setSelection(i); 
       break; 
      } 

     } 
    } 

這裏函數接受要設置所選的值,第二參數Spinner是價值你想選擇

什麼

你可以調用

selectValue(spinnnerName,"800"); 
+1

N J:謝謝你的rply。可能它工作讓我檢查 – Shiv

0

你有寬度的數組:R.array.width_array

,你可能會有高度的數組:R.array.height_array

ArrayAdapter<CharSequence> adapter_width = ArrayAdapter.createFromResource(this,    
       R.array.width_array,R.layout.spinner_item);  
    adapter_width.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

spin_width.setAdapter(adapter_width); 
spin_width.setOnItemSelectedListener(new OnItemSelectedListener() { 

@Override 
public void onItemSelected(AdapterView<?> parent, View view, 
        int position, long id) { 
     // TODO Auto-generated method stub 

    String width = parent.getItemAtPosition(position).toString(); 
    mWidth = Integer.valueOf(width); 
    Log.d(TAG, "Spinner1"+mWidth); 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
      // TODO Auto-generated method stub 

      } 
    }); 

TO:

ArrayAdapter<CharSequence> adapter_width = ArrayAdapter.createFromResource(this,    
       R.array.width_array,R.layout.spinner_item);  
    adapter_width.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 


//Adapter Height functionality: 
    ArrayAdapter<CharSequence> adapter_height = ArrayAdapter.createFromResource(this,    
        R.array.height_array,R.layout.spinner_item);  
     adapter_height.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

spin_width.setAdapter(adapter_width); 

spin_height.setAdapter(adapter_height); // set Adapter of height on spinner height 
spin_width.setOnItemSelectedListener(new OnItemSelectedListener() { 

@Override 
public void onItemSelected(AdapterView<?> parent, View view, 
        int position, long id) { 
     // TODO Auto-generated method stub 

    spin_height.setSeletion(position); 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
      // TODO Auto-generated method stub 

      } 
    });