[英]How to dismiss the spinner with click on outside of the dialog?
如何通过单击对话框外部来关闭对话框微调器?
我在nexus 7(2013)中尝试过它,我可以单击对话框微调器外部以关闭它,而无需添加任何片段代码,但是在Samsung Tablet中不起作用,所以我想知道如何关闭它或我错过添加的内容。
并且我找到了有关此Android的一些链接-触摸外部视图后如何关闭注入微调器的AlertDialog? 。 但是没有Object-Spinner的setCancelable(true)方法。
<Spinner
android:id="@+id/dialog_spinner"
android:layout_width="100dip"
android:layout_height="38ip"
android:spinnerMode="dialog"
/>
谢谢。
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) v.setVisibility(View.GONE);
}
如果您没有setCancelable方法或setCanceledOnTouchOutside,请使用以下功能:
public static void clickOutSideSpinner(View view)
{
// Configure touch listener for all views except edittext,Button and Spinner
if (!(view instanceof EditText)
&&!(view instanceof Button)
&&!(view instanceof Spinner))
{
view.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
//here you close your dialog spinner
return false;
}
});
}
//runs through all the children views .
if (view instanceof ViewGroup)
{
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)
{
View innerView = ((ViewGroup) view).getChildAt(i);
closeSlidingDrawerOnTouch(innerView);
}
}
}
在onCreate调用函数并传递布局:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textviw1 = (TextView) findViewById(R.id.textviw1);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Button button1 = (Button) findViewById(R.id.button1);
//change to other layout if you use other
LinearLayout layout1 = (LinearLayout) findViewById(R.id.layout1);
clickOutSideSpinner(layout1);
}
AlertDialog.Builder具有setCancelable(boolean cancelable)方法,在对话框外单击时,需要传递true来关闭对话框。
确保包含微调框的对话框具有可取消属性设置为true
有关此属性的说明,请参见http://developer.android.com/reference/android/app/AlertDialog.Builder.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.