您可以通过创建自定义微调器来获得它,
创建布局后,您应该在布局中使用它,
// com.talha.examples 是包名
mySpinner.java
package com.talha.examples;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Spinner;
import android.widget.Toast;
public class mySpinner extends Spinner {
Context context;
public mySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if(visibility == View.VISIBLE)
Toast.makeText(this.context, "VISIBLE NOW", 1000).show();
if(visibility == View.GONE)
Toast.makeText(this.context, "HAS GONE", 1000).show();
}
}
activity_main_spinner.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.talha.examples.mySpinner
android:id="@+id/mySpinner"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="#ffffaa" >
</com.talha.examples.mySpinner>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/mySpinner"
android:text="Button" />
</RelativeLayout>
主要活动.java
导入android.app.Activity;导入android.os.Bundle;导入android.view.View;导入 android.view.View.OnClickListener;导入android.widget.Button;
公共类 MainActivity 扩展 Activity { Button 按钮;com.talha.examples.mySpinner mySpinner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_spinner);
mySpinner = (com.talha.examples.mySpinner) findViewById(R.id.mySpinner);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mySpinner.getVisibility() == View.VISIBLE) {
mySpinner.setVisibility(View.GONE);
} else {
mySpinner.setVisibility(View.VISIBLE);
}
}
});
}
}