1

我想在选择“其他”选项时输入文本

Spinner spn;
........
adapterString = new String[]
{
    "A",
    "B",
    "Other"
};
ArrayAdapter<String> adptLubricatorType = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, adapterString);
spn.setAdapter(adptLubricatorType);
spn.setOnItemSelectedListener(listenerspn);
private Spinner.OnItemSelectedListener listenerspn = new Spinner.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {


    }

当我拉下微调器并选择“其他”时,如何插入文本?

我希望我可以像edittext一样输入,当我点击其他东西时,“Other”会变成我刚刚输入的字符串

4

3 回答 3

1

也许创建一个警报对话框并向其中添加编辑文本:

final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
alert.setView(input);
于 2013-07-31T04:19:58.850 回答
0

总是在 XML 文件中EditText设置它的可见性。INVISIBLE然后当您检测到所选项目为"Other"时,将 VISIBILITY 设置为VISIBLE。就像是

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
   //get the item selected item. Compare to the itme for which you want to make the EditText visible
   if( spn.getSelectedItem().toString().equals("Other"){
       yourEditText.setVisibility(View.VISIBLE);
   }

}       

然后你可以EditText像往常一样处理onClickListener等。希望你得到这个概念,否则请发表评论。

于 2013-07-31T04:29:30.530 回答
0

用于setVisibility()切换编辑文本的可见性。这是工作代码片段,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    other = (EditText)findViewById(R.id.other);
    other.setVisibility(View.GONE);
    Spinner spn = (Spinner)findViewById(R.id.spinner1);
    String[] adapterString = new String[]
    {
       "A",
       "B",
       "Other"
    };
    ArrayAdapter<String> adptLubricatorType = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, adapterString);
    spn.setAdapter(adptLubricatorType);

    Spinner.OnItemSelectedListener listenerspn = new Spinner.OnItemSelectedListener() {

}

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent>


    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="wrap_content" />

    <EditText 
        android:id="@+id/other"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"/>


</LinearLayout>
于 2013-07-31T05:20:04.500 回答