我在我的应用中实现了这个微调器。我正在显示一个对话框,我在该对话框中有我的微调器。当对话框出现时,我希望我的微调器打开。我已经在setAdapter()方法之后尝试了performClick()事件,但它似乎不起作用。
我的代码如下所示:
final MaterialBetterSpinner selectBrandForSODRating = dialog.findViewById(R.id.selectBrandForSODspinner);
final ArrayList<Products> brandList = new ArrayList<Products>();
Cursor crsCheckSODData = database.rawQuery(myQuery, null);
if(crsCheckSODData.getCount() > 0){
while (crsCheckSODData.moveToNext()) {
//data...
brandList.add(data);
}
}
crsCheckSODData.close();
final ArrayAdapter<Products> SODBrandAdapter = new ArrayAdapter<Products>(myView.this, android.R.layout.simple_dropdown_item_1line, brandList);
selectBrandForSODRating.setAdapter(SODBrandAdapter);
selectBrandForSODRating.performClick(); //this right here isnt working...
答案 0 :(得分:0)
如果您使用的是https://github.com/Lesilva/BetterSpinner,则代码会覆盖onTouchEvent
here,但不会覆盖onClick
。这就是performClick
不起作用的原因。
您可以模拟触摸事件而不是视图。以下是:
int[] coords = new int[2];
selectBrandForSODRating.getLocationOnScreen(coords);
float x = (float) coords[0];
float y = (float) coords[1];
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
int metaState = 0;
// Obtain MotionEvent object
long downTime1 = SystemClock.uptimeMillis();
long eventTime1 = SystemClock.uptimeMillis() + 100;
MotionEvent motionEventDown = MotionEvent.obtain(
downTime1,
eventTime1,
MotionEvent.ACTION_DOWN,
x,
y,
metaState
);
// Dispatch touch event to view
selectBrandForSODRating.dispatchTouchEvent(motionEventDown);
long downTime2 = SystemClock.uptimeMillis();
long eventTime2 = SystemClock.uptimeMillis() + 100;
MotionEvent motionEventUp = MotionEvent.obtain(
downTime2,
eventTime2,
MotionEvent.ACTION_UP,
x,
y,
metaState
);
// Dispatch touch event to view
selectBrandForSODRating.dispatchTouchEvent(motionEventUp);
您也可以直接从here
调用方法selectBrandForSODRating.requestFocus();
selectBrandForSODRating.showDropDown();
但我不推荐它,因为还有另一个布尔状态isPopUp,它具有私人访问权限,您无法访问。