Android 带有 MultiAutoCompleteTextView 的微调器?
Android Spinner With MultiAutoCompleteTextView?
- 我正在做一个小 Android 应用程序。
- 因为我有一个带有项目列表的微调器(前品牌,颜色)。
- 我还有 MultiAutoCompleteTextView。
- 并且我创建了两个自定义阵列适配器颜色适配器和品牌适配器。
- 颜色适配器保存与颜色相关的值,品牌适配器保存与品牌相关的值。
- 这里我要做的是验证微调器中的选定项目,例如如果我 select 微调器中的颜色然后在 MultiAutoCompleteTextView 中它应该给我颜色适配器中的值。如果我 select Brand In Spinner Then Values In Brand adapter.
- 谁能帮我锄头做这个...
- 这是我到目前为止尝试过的代码。
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.util.Log;
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"};
String[] brand={"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 clistAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, color);
ArrayAdapter blistAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, brand);
text1.setAdapter(clistAdapter);
text1.setAdapter(blistAdapter);
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) {
String text = spinner.getSelectedItem().toString();
Log.d("!!!!!!!!!!!!", "onItemSelected: "+text);
}
@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(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = spinner.getSelectedItem().toString();
Log.d("!!!!!!!!!!!!", "onItemSelected: "+text);
if(text.equals("Color")){
text1.setAdapter(clistAdapter);
} else if(text.equals("Brand")){
text1.setAdapter(blistAdapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
- 我正在做一个小 Android 应用程序。
- 因为我有一个带有项目列表的微调器(前品牌,颜色)。
- 我还有 MultiAutoCompleteTextView。
- 并且我创建了两个自定义阵列适配器颜色适配器和品牌适配器。
- 颜色适配器保存与颜色相关的值,品牌适配器保存与品牌相关的值。
- 这里我要做的是验证微调器中的选定项目,例如如果我 select 微调器中的颜色然后在 MultiAutoCompleteTextView 中它应该给我颜色适配器中的值。如果我 select Brand In Spinner Then Values In Brand adapter.
- 谁能帮我锄头做这个...
- 这是我到目前为止尝试过的代码。
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.util.Log;
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"};
String[] brand={"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 clistAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, color);
ArrayAdapter blistAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, brand);
text1.setAdapter(clistAdapter);
text1.setAdapter(blistAdapter);
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) {
String text = spinner.getSelectedItem().toString();
Log.d("!!!!!!!!!!!!", "onItemSelected: "+text);
}
@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(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = spinner.getSelectedItem().toString();
Log.d("!!!!!!!!!!!!", "onItemSelected: "+text);
if(text.equals("Color")){
text1.setAdapter(clistAdapter);
} else if(text.equals("Brand")){
text1.setAdapter(blistAdapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}