나는 내 아이템에 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);
}
. 도움 주셔서 감사합니다.
'editextObject.setVisibility (View.VISIBLE는) '귀하의 경우에 작동합니다. 선택한 항목의 값이 '기타'인 조건에 추가합니다. –
@ShashankKadne 음 .. 답장을 보내 주셔서 감사합니다. 그러나 실제로 특정 항목을 선택할 때 위 코드를 수행하는 방법에 대해 혼란 스럽습니다. –
'spinServiceType.getSelectedItem(). toString();'은 "Others"와 비교할 수있는 텍스트를 제공해야합니다. 아니면이 경우에는'spinnerServiceType' 값도 사용할 수 있습니다. –