2017-09-16 45 views
1

我想创建一个具有多项选择的Spinner,并且每个选择的都必须添加到Spinner box.my spinner具有dropDown List.pictures将描述我想要创建的内容:将Spinner的每一个选项添加到文本视图

text 1 is selected

text 1 and text 2 are selected

text 1 and 2 and 3 are selected

我如何创造这个微调???

+0

不这样做,如果您选择100项?你想在你的屏幕上看到他们所有的几行吗? – pskink

+0

@pskink我想限制选择只有三个项目! –

回答

2

试试这个 你可以使用TextView.append()方法。

方便的方法来指定文本切片追加到的TextView的显示缓冲区,它升级为可编辑的,如果它是不是已经编辑。

示例代码

textView = (TextView) findViewById(R.id.text); 
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
     String s=destspin1.getSelectedItem().toString(); 
     textView.append(s); 

     /* or try this 
     String str=textView.getText().toString().trim(); 
     textView.setText(str+s);*/ 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parentView) { 
     // your code here 
    } 

}); 
+0

我没有完全理解你的解决方案! tvSchool是Spinner的TextView?我试过了,但目前为止还没有工作,你能解释一下吗? –

+0

@AzinNilchi替换** tvSchool **与你的** Textview ** –

+0

我试过了,把你说的使用append的微调文本视图,但它也会崩溃!与此错误: java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView.append(java.lang.CharSequence)'对空引用 –

1

我创建一个完整的代码,你只申请

XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_test_code" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.ncrypted.thumbpin.TestCode"> 

    <Spinner 
     android:layout_marginTop="20dp" 
     android:id="@+id/spinner" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 


    <TextView 
     android:id="@+id/text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:text="dsasas" 
     android:textColor="@color/black" 
     android:textSize="20dp" /> 
</LinearLayout> 

JAVA

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.TextView; 

import java.util.ArrayList; 

public class TestCode extends AppCompatActivity { 

    Spinner spinner; 
    TextView textView; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_test_code); 

     textView = (TextView) findViewById(R.id.text); 
     spinner = (Spinner) findViewById(R.id.spinner); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"String 1", "String 2", "String 3", "String 4", "String 5"}); 
     spinner.setAdapter(adapter); 

     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
       String s = spinner.getSelectedItem().toString(); 
       textView.append(s); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parentView) { 
       // your code here 
      } 

     }); 

    } 
} 

注:,如果你想在一次添加特定的项目应该比你必须使用ArrayList来选择项存储库中数组列表 和循环数组和检查项目已添加或不添加

+0

我想你不明白我的问题。我不希望单独的textView显示这些选择!我想在Spinner Itself中显示Spinner选择!像我展示的图像 –

相关问题