2013-07-25 1 views
1

나는 내 아이템에 Spinner이라는 아이템을 가지고 있습니다. 나는 내가 "보이지 않는"것으로 설정 한 가시성을 EditText 표시해야합니다. 스피너에서 "기타"항목을 선택하면이 EditText을 표시해야합니다.선택한 스피너 아이템에 텍스트 수정 편집보기

여기 내 코드입니다 : - 나는 "기타"항목을 클릭 할 때 ETServiceType를 표시해야

Activity class:- 




     protected void onCreate(Bundle savedInstanceState) 
       { 
        // TODO Auto-generated method stub 
        super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.maintenance); 

        ETServiceStation = (EditText)findViewById(R.id.ETServiceStation); 
        ETServiceType = (EditText)findViewById(R.id.ETServiceType); 
        ETServiceCost= (EditText)findViewById(R.id.ETServiceCost); 

        spinServiceType = (Spinner)findViewById(R.id.spinServiceType); 
        btnMaintenenceSave= (ImageButton)findViewById(R.id.btnMaintenenceSave); 
        btnMaintenanceClear = (ImageButton)findViewById(R.id.btnMaintenenceClear); 

        spinServiceType.setOnItemSelectedListener(this); 

        btnMaintenanceClear.setOnClickListener(this); 
        btnMaintenenceSave.setOnClickListener(this); 

       ArrayAdapter<String> serviceTypeAdapter = new ArrayAdapter<String>(getApplicationContext(), R.array.service_type_array, android.R.layout.simple_spinner_item); 
       serviceTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       spinServiceType.setAdapter(serviceTypeAdapter); 


       } 



    public void onItemSelected(AdapterView<?> parent, View view, int servicePos, long id) 
    { 
     // TODO Auto-generated method stub 
     spinnerServiceType = spinServiceType.getItemAtPosition(servicePos).toString(); 


     if(spinnerServiceType.matches("Others")) 
     { 
      ETServiceType = (EditText)findViewById(R.id.ETServiceType); 
      ETServiceType.setVisibility(View.VISIBLE); 
      spinnerServiceType = ETServiceType.getText().toString();  
     } 
     serviceType = spinnerServiceType; 
    } 
     } 



    Resource:- 

     <?xml version="1.0" encoding="utf-8"?> 
     <resources> 
      <string-array name="service_type_array"> 
       <item>Full Service</item> 
       <item value="serviceother">Other</item> 

      </string-array> 
     </resources> 




    maintenance.xml 

     <?xml version="1.0" encoding="utf-8"?> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:padding="30dp"> 

      <TextView 
       android:id="@+id/txtMTitle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Maintenance" 
       android:layout_gravity="center_horizontal" 
       android:textSize="25sp" 
       android:textColor="@color/SteelBlue" 
       android:typeface="sans"/> 


      <TextView 
       android:layout_marginTop="60dp" 
       android:id="@+id/txtServiceStation" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Service station name:" /> 

      <EditText 
       android:id="@+id/ETServiceStation" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:ems="10" > 

       <requestFocus /> 
      </EditText> 

      <TextView 
       android:id="@+id/txtServiceType" 
       android:layout_marginTop="10dp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Service type: "/> 

      <Spinner 
       android:id="@+id/spinServiceType" 
       android:layout_marginTop="10dp" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" /> 

      <EditText 
       android:id="@+id/ETServiceType" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:inputType="numberDecimal" 
       android:visibility="invisible" 
       android:ems="10" /> 

      <TextView 
       android:id="@+id/txtServiceCost" 
       android:layout_marginTop="10dp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Servicing expence:" /> 

      <EditText 
       android:id="@+id/ETServiceCost" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:inputType="numberDecimal" 
       android:ems="10" /> 

        <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:layout_marginTop="40dp" 
          android:layout_marginLeft="50dp" 
          android:orientation="horizontal"> 
           <ImageButton 
             android:id="@+id/btnMaintenenceSave" 
             android:layout_width="wrap_content" 
             android:layout_height="wrap_content" 
             android:background="@color/white" 
             android:src="@drawable/fuelbtnsaveclick" /> 

           <ImageButton 
            android:id="@+id/btnMaintenenceClear" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_marginLeft="20dp" 
            android:background="@color/white" 
            android:src="@drawable/fuelbtnclearclick" /> 

        </LinearLayout> 
     </LinearLayout> 


     Database: 
     public long createMainEntry(MaintenanceExpense me) 
    { 
     ContentValues mcv = new ContentValues(); 
     mcv.put(KEY_SERVICE_STATION,me.getServiceStation()); 
     mcv.put(KEY_SERVICE_TYPE,me.getServiceType()); 
     mcv.put(KEY_SERVICE_COST,me.getServiceCost()); 
     return otherInfoDatabase.insert(MAINTENANCE_DATABASE_TABLE, null, mcv); 
    } 

. 도움 주셔서 감사합니다.

+0

'editextObject.setVisibility (View.VISIBLE는) '귀하의 경우에 작동합니다. 선택한 항목의 값이 '기타'인 조건에 추가합니다. –

+0

@ShashankKadne 음 .. 답장을 보내 주셔서 감사합니다. 그러나 실제로 특정 항목을 선택할 때 위 코드를 수행하는 방법에 대해 혼란 스럽습니다. –

+0

'spinServiceType.getSelectedItem(). toString();'은 "Others"와 비교할 수있는 텍스트를 제공해야합니다. 아니면이 경우에는'spinnerServiceType' 값도 사용할 수 있습니다. –

답변

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

      spinnerServiceType = spinServiceType.getItemAtPosition(position).toString(); 
      //if spinnerServiceType 's value is other 
//then set visibility of textview visible put text watcher on it so u can get text when user finished ..if u dont want then u can use simple getText then change its visibility 
//As u are setting your array value from resource you cant update it programmatically 
//so first of all u have to take its array items in list . so u can update this list after user enter the value in edit text 


     } 
+0

답변 주셔서 감사합니다 .. 잘 작동합니다! :) –

+0

도와 드리겠습니다 !! –

4
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
{  
    if(spinServiceType.getItemAtPosition(position).toString().equalsIgnorecase("Others") 
    { 
     editextObject.setVisibility(View.VISIBLE); 
    } 
} 
+0

잘 작동 .. 감사합니다 –

+0

@ Pooja Gaonkar : 당신은 환영합니다, 해피 코딩. –

+0

나는 또 하나의 의심을 품었다. 텍스트를 입력하고 데이터베이스에 저장하면 값이 저장되지 않는다. 반면에 스피너에서 값을 선택하면 ("Others"제외) 값이 올바르게 저장 됨 –

관련 문제