我是Android尝试小应用程序的新手。
我有一个带有项目列表的微调器,并且我具有MultiAutoCompleteTextView。
当我从微调器中选择任何一项时,它应该显示与该项目相关的内容。例如,如果我选择“品牌”,则它应允许MultiAutoCompleteTextView中与品牌(Adidas,Lee,Puma)相关的项目。
如果我选择颜色(蓝色,黑色,黄色,绿色),例如那样。
这是我的活动……
package com.example.ns2.testing;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity {
String[] color={"Blue","Black","Red","Yellow","Green","Pink","Sony","LG","Samsung"};
MultiAutoCompleteTextView text1;
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1= (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
ArrayAdapter listAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, color);
text1.setAdapter(listAdapter);
text1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
spinner = (Spinner) findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this, R.array.SearchKey, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
/* @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}*/
/*@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}*/
});
}
}
这是我的布局……
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.ns2.testing.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/like"
android:textSize="@dimen/abc_action_bar_progress_bar_size"
android:layout_alignParentLeft="true"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/like"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="70dp"
android:layout_marginEnd="200dp">
</Spinner>
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Value.... "
android:layout_alignTop="@+id/spinner"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
有人可以帮我解决这个问题吗?
感谢他们.....
最佳答案
使用:spinner.setOnItemSelectedListener
我创建了类别和子类别微调器。根据类别选择显示子类别值。以下是代码示例:
spn_category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String selection = spn_category.getSelectedItem().toString();
// Based on this selection category, you can select which subcategory to show
ArrayAdapter<String> selection_subcat = new ArrayAdapter<String>(ServiceProviderProfileEdit.this,
android.R.layout.simple_spinner_dropdown_item, CategoryInfo.getSubCategories(selection));
spn_subcategory.setAdapter(selection_subcat);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here (optional)
}
});
希望对您有帮助。