我在我的一个活动上有一个微调器,在初始化时将为spinner.setSelection(0)从数据库加载数据。当我更改微调器值时,假设是spinner.setSelection(1),我希望在加载数据之前清除EdittText ed1视图中的文本(如果存在)或将其保留为空/null/空
下面是我的代码片段。
public class TestActivity extends Activity implements
AdapterView.OnItemSelectedListener {
...
Spinner spinner;
EditText ed1;
...
protected void onCreate(Bundle savedInstanceState) {
...
...
spinner = (Spinner) findViewById(R.id.spinner1);
ed1 = (EditText)findViewById(R.id.ed1);
...
adapter = new ArrayAdapter<String>(this,R.layout.temp_layout01,R.id.temptxt01,xNames);
spinner.setAdapter(adapter);
spinner.setSelection(getSpinnerIndex(spinner,mname));
spinner.setOnItemSelectedListener(this);
}方法来清除EditText数据。
private void clearAllFields() {
Log.i(TAG, "Clearing Fields");
ed1.getText().clear(); //test that didn't clear the data
ed1.setText(null); // test that didn't clear the data
}微调监听器的onItemSelected
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
clearAllFields();
mname = parent.getItemAtPosition(position).toString();
mID = dbHelper.getCurrentData(mname);
spinner.setSelection(getSpinnerIndex(spinner,mname));
populateData();
}ed1在TableRow内
<TableRow
android:id="@+id/tbHole18TR"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:layout_width="50dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/ed1"
android:background="@color/Green3"
android:textAlignment="center" />
</TableRow>这跟focusChange等有什么关系吗?
发布于 2017-03-15 20:24:31
试试这个逻辑
https://stackoverflow.com/questions/42789870
复制相似问题