如何通过单击对话框外部来关闭对话框微调器?
我在nexus 7(2013)中试用它,我可以点击对话框微调器的外部来解除它而不在片段中添加任何代码但在三星平板电脑中不起作用,所以我想知道如何解雇它或我想错的东西
我发现了一些关于此Android - How an AlertDialog injected with spinner can be closed when touched outer view?的链接。 但是Object - Spinner没有setCancelable(true)方法。
<Spinner
android:id="@+id/dialog_spinner"
android:layout_width="100dip"
android:layout_height="38ip"
android:spinnerMode="dialog"
/>
感谢。
答案 0 :(得分:0)
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) v.setVisibility(View.GONE);
}
答案 1 :(得分:0)
如果你没有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);
}
答案 2 :(得分:0)
AlertDialog.Builder有setCancelable(boolean cancelable)方法,你需要传递true来关闭你在对话框外点击的对话框。
确保包含微调器的对话框的cancelable属性设置为true
参考http://developer.android.com/reference/android/app/AlertDialog.Builder.html 这个属性描述。