Mar*_*kis 10 android view handler spinner
我有一个带有微调器的活动,我想知道是否有可能以编程方式关闭微调器,如果用户已打开它.
整个故事是在后台我在一个单独的线程上运行一个进程.当进程完成时,我在主要活动上调用一个Handler,并根据结果执行一些任务.然后,我想关闭微调器,用户打开它.
微调器位于main.xml布局中:
<Spinner android:id="@+id/birthPlaceSpinner" android:layout_weight="1"
android:layout_height="wrap_content" android:prompt="@string/select"
android:layout_width="fill_parent" />
Run Code Online (Sandbox Code Playgroud)
这是处理程序:
private class BirthplaceChangedHandler extends Handler {
@Override
public void handleMessage(Message msg) {
String placeFilterStr = birthPlaceFilterText.getText().toString();
if ("".equals(placeFilterStr) || placeFilterStr == null || validNewAddresses.isEmpty()) {
birthPlaceSpinner.setEnabled(false);
hideGeoLocationInformation();
} else {
birthPlaceSpinner.setEnabled(true);
}
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.multiline_spinner_dropdown_item, validNewAddressesStr)
birthPlaceSpinner.setAdapter(adapter);
}
}
Run Code Online (Sandbox Code Playgroud)
干杯!
小智 17
这对我有用:
class SomeAdapter extends BaseAdapter implements SpinnerAdapter {
//......
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
//......
}
view.setOnClickListener(new ItemOnClickListener(parent));
return view;
}
//.....
}
Run Code Online (Sandbox Code Playgroud)
和点击监听器:
class ItemOnClickListener implements View.OnClickListener {
private View _parent;
public ItemOnClickListener(ViewGroup parent) {
_parent = parent;
}
@Override
public void onClick(View view) {
//.......
// close the dropdown
View root = _parent.getRootView();
root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}
}
Run Code Online (Sandbox Code Playgroud)
小智 11
public static void hideSpinnerDropDown(Spinner spinner) {
try {
Method method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
method.setAccessible(true);
method.invoke(spinner);
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
好吧它比我想象的要复杂一点.
我在这里添加了一步一步的细节.尝试遵循它.我能够在api 10级实现这一目标.
此解决方案假定您应该在用户单击主页按钮时以编程方式关闭提示对话框,或者如果您必须在没有用户交互的情况下转到下一个活动
第一步是通过扩展Spinner类来创建Custom Spinner.比方说,我创建了一个名为类CustomSpinner在包com.bts.sampleapp
我的CustomSpinner类看起来像这样,
package com.bts.sampleapp;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Spinner;
public class CustomSpinner extends Spinner{
Context context=null;
public CustomSpinner(Context context) {
super(context);
this.context=context;
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
}
Run Code Online (Sandbox Code Playgroud)
现在在您的Xml文件中,用此自定义微调器替换Spinner元素,
<com.bts.sampleapp.CustomSpinner
android:id="@+id/spin"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
下一步是在Activity类中初始化并设置适配器到此微调器,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomSpinner spin=null;
spin=(CustomSpinner)findViewById(R.id.spin);
spin.setAdapter(spinnerAdapter); //you can set your adapter here.
}
Run Code Online (Sandbox Code Playgroud)
最后一步是在用户单击HomeButton或当Activity移动到后台时关闭对话框.为此,我们像这样重写onPause(),
@Override
protected void onPause() {
Log.i("Life Cycle", "onPause");
spin.onDetachedFromWindow();
super.onPause();
}
Run Code Online (Sandbox Code Playgroud)
现在在onPause()中调用spin.onDetachedFromWindow();为您关闭提示对话框的方法.
现在要说,spin.onDetachedFromWindow();从Activity中的任何地方进行调用应该可以帮助您以编程方式关闭微调器.所以,如果这是你想要的,那么删除onpause().
我没有看到实现这一目标的方法 - 没有方法Spinner可以关闭它.a的"开放"部分Spinner是AlertDialogAndroid 1.x和2.x,我不完全确定在使用全息主题时它是如何在Honeycomb上实现的.
唯一的解决方法是克隆源Spinner代码并自己添加一些代码来关闭对话框.但是,再次,它将无法在Honeycomb或更高版本上工作,直到您可以看到并克隆该代码.
除此之外,我认为你想要的是糟糕的用户体验.如果用户打开了Spinner,他们很可能会主动检查Spinner内容并进行选择.从他们的手指下面说出来,最多会让他们感到困惑.请考虑另一种方法.
此外,getApplicationContext()除非您知道自己使用的原因,否则请勿使用getApplicationContext().getApplicationContext()创建时,您不需要甚至不需要ArrayAdapter.
| 归档时间: |
|
| 查看次数: |
14377 次 |
| 最近记录: |