从android中的Multiautocomplete向数组添加值

时间:2016-07-20 05:10:39

标签: android arrays arraylist multiautocompletetextview

如何使用MultiAutoCompleteTextView从用户获取多个条目,以便将每个条目添加到单个字符串中,并且每个字符串都应存储在字符串数组中。

1 个答案:

答案 0 :(得分:2)

@William Willi

检查以下

您需要使用split()方法,因为我们CommaTokenizer()使用MultiAutoCompleteTextView

所以你的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    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="iifl.animesh.cuser.progressbarexample.MainActivity">

    <MultiAutoCompleteTextView
        android:id="@+id/multitoken"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/submit"
        android:layout_below="@+id/multitoken"
        android:text="Submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</RelativeLayout>

在你的mainActivity中

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private MultiAutoCompleteTextView multiAutoCompleteTextView;

    // your array for the autocomplete

    private static final String[] COUNTRIES = new String[] {
            "India","Belgium", "France", "Italy", "Germany", "Spain","USA","China"
    };

    private Button mbutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multitoken);
        mbutton = (Button) findViewById(R.id.submit);

        // setting listener for button
        mbutton.setOnClickListener(this);

        //setting adapter for multiAutoCompleteTextView
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);
        multiAutoCompleteTextView.setAdapter(adapter);
        multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());


    }

    @Override
    public void onClick(View view) {
        String text = multiAutoCompleteTextView.getText().toString();

        if(text != null && text.length() >0) {
            text = text.substring(0, text.length() - 1);
        String countries[] = text.split(",");  // countries array will have all the countries entered in multiAutoCompleteTextView

        for(String s : countries) {
            Toast.makeText(this,"Countries are = "+s,Toast.LENGTH_SHORT).show();
        }
        }
        else {
           Toast.makeText(this,"Please enter text",Toast.LENGTH_SHORT).show();
       }

    }
}

我希望这会有所帮助