[英]How to lock the spinner after button click in android
How can I prevent the user to change the value selected in spinner
after some event like button press? 在按下按钮之类的事件后,如何防止用户更改
spinner
选择的值?
It is like after selecting the desired value from spinner
the user will press a button
after which they will not be allowed to see the values in the spinner
except the top value ie the selected one so that no change be made in the selected value. 就像在从
spinner
选择所需的值之后,用户将按下一个button
此后将不允许他们查看spinner
除最高值(即选定的值)之外的值,从而不会更改选定的值。
UPDATE:- And yes the spinners
are also created dynamically on the press of that same button
so it should lock only the previously created spinners not the new one. 更新: -是的
spinners
也被动态创建上的按下same button
,所以应该只锁定先前创建的微调不是新的。
Code for dynamic creation of Layout
which contains the Spinner
along with an EditText
& a Button
:- 动态创建
Layout
代码,其中包含Spinner
以及EditText
和Button
:
View.OnClickListener addListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
final RelativeLayout newView = (RelativeLayout) getLayoutInflater().inflate(R.layout.product_row_detail, null);
newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
btnRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
container.removeView(newView);
}
});
container.addView(newView);
简单只需在按钮单击上禁用微调器
spinner.setEnabled(false);
In your onClick method, you can just set the visibility to GONE to remove the view as shown below: 在onClick方法中,您只需将可见性设置为GONE即可删除视图,如下所示:
View.OnClickListener addListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
final RelativeLayout newView = (RelativeLayout) getLayoutInflater().inflate(R.layout.product_row_detail, null);
newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
btnRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newView.setVisibility(View.GONE); // remove (hide) your view
}
});
container.addView(newView);
And to enable it again, you can set its visibility to VISIBLE as shown below: 并再次启用它,可以将其可见性设置为VISIBLE,如下所示:
newView.setVIsibility(View.VISIBLE);
Or you could just show/hide your spinner as shown below: 或者您可以只显示/隐藏微调器,如下所示:
your_spinner.setVisibility(View.GONE); // to remove (hide)
your_spinner.setVisibility(View.VISIBLE); // to make it visible
If you want to only disable the spinner: 如果只想禁用微调器:
your_spinner.setEnabled(false);
Hope it helps. 希望能帮助到你。
You need to create a variable (may be boolean flag = true
) 您需要创建一个变量(可能是
boolean flag = true
)
On button click change to flag = false
在按钮上单击更改为
flag = false
And inside Listener to Spinner check if flag is true of false 然后在Spinner的Listener内检查flag是否为true或false
Example 例
//inside listener of spinner //微调器的内部监听器
if(flag){
//do task
}else{
//restrict the task or don't do anything or display message
}
For Updated question 对于更新的问题
Then you should use disable() method. 然后,您应该使用disable()方法。 spinner1.disable() or spinner2.disable() or so on..
spinner1.disable()或spinner2.disable()等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.