当我单击EditText时,我希望微调器打开菜单。当我单击textviewplaces时,我希望打开微调器菜单。我该怎么办?
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnItemSelectedListener {
private Spinner spinnerDevice;
private static final String[] paths = {"......"};
TextView textviewPlaces;
spinnerDevice = (Spinner)findViewById(R.id.spinnerDevice);
ArrayAdapter<String>adapterSpinnerDevice = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
textviewPlaces = findViewById(R.id.places_textView);
adapterSpinnerDevice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDevice.setAdapter(adapterSpinnerLocation);
spinnerDevice.setOnItemSelectedListener(this);
XML文件:
<AutoCompleteTextView
android:id="@+id/places_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="44dp"
android:layout_marginTop="31dp"
android:text="TextView"
android:drawableLeft="@drawable/location"
android:drawablePadding="2dp"
android:ems="10"
android:textStyle="bold" />
答案 0 :(得分:0)
使用AutoCompleteTextView代替
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_dropdown_item_line,
paths);
final AutoCompleteTextView textView;
textView = (AutoCompleteTextView) findViewById(R.id.textView);
textView.setAdapter(arrayAdapter);
/* textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View arg0) {
textView.showDropDown();
}
}); */
在布局文件中定义UI元素
<AutoCompleteTextView
android:id="@+id/textView"
android:layout_width="match_content"
android:layout_height="wrap_content"
android:ems="10" />
答案 1 :(得分:0)
在您代码下方的onCreate()
中添加此代码-
textviewPlaces.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
spinnerDevice.performClick();
}
});
点击spinner
,它将打开您的textview
。
答案 2 :(得分:0)
如果您想要这样的图片 AutoCompeleteTextView Exmaple
Arwy Shelke的答案可以为您提供帮助,但是如果您想打开一个真正的微调器,则可以使用以下代码:
spinner.performClick();