Java 迭代单击按钮上的侦听器
我有一个ImageView,我已经分配了一个单击侦听器。我试图找出如何根据用户在循环中的位置为侦听器提供一个新函数。例如,第一次单击将显示一个文本视图,第二次单击将显示另一个文本视图,第三次单击将同时隐藏两者Java 迭代单击按钮上的侦听器,java,android,switch-statement,Java,Android,Switch Statement,我有一个ImageView,我已经分配了一个单击侦听器。我试图找出如何根据用户在循环中的位置为侦听器提供一个新函数。例如,第一次单击将显示一个文本视图,第二次单击将显示另一个文本视图,第三次单击将同时隐藏两者 public void AddOption(View view) { int index = 2; switch (index) { case 0: // if we are using index
public void AddOption(View view) {
int index = 2;
switch (index) {
case 0:
// if we are using index 0, set the text to index 1 text and change index to 1
index = 1;
findViewById(R.id.pollOption3TextInputLayout).setVisibility(View.VISIBLE);
break;
case 1:
index = 2;
findViewById(R.id.pollOption4TextInputLayout).setVisibility(View.VISIBLE);
break;
case 2:
index = 0;
findViewById(R.id.pollOption3TextInputLayout).setVisibility(View.GONE);
findViewById(R.id.pollOption4TextInputLayout).setVisibility(View.GONE);
break;
}
}
我怎样才能做到这一点呢?将索引置于方法之外(最简单的方法):
将索引保留在方法之外(最简单的方法):
创建实现View.OnClickListener接口的自定义类。然后向该类添加一个index integer属性,以及一个用于更改其他类的值的setter 公共类CustomClickListener实现View.OnClickListener{
private Integer index;
public CustomClickListener() {
this.index = 0;
}
public void onClick(View v) {
switch (this.index) {
case 0:
//Do wat yo want when index is 0
break;
case 1:
//Do wat yo want when index is 1
break;
case 2:
//Do wat yo want when index is 2
break;
}
}
public void setIndex(Integer index) {
this.index = index;
}
}
然后,在自定义click listener类的活动上实例化一个属性,并将其设置为视图。执行此操作后,如果在任何位置设置此侦听器的索引值,则在单击视图时,索引值将具有正确的值
public class YourActivityClassWithTheViewAndTheIndex {
private View yourView;
private CustomClickListener customClickListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.customClickListener = new CustomClickListener()
this.yourView.setOnClickListener(this.customClickListener);
//iterate over the index, or whatever, and set it to the listener
this.customClickListener.setIndex(3);
}
}
我希望这会有所帮助!创建一个实现View.OnClickListener接口的自定义类。然后向该类添加一个index integer属性,以及一个用于从其他类更改其值的setter 公共类CustomClickListener实现View.OnClickListener{
private Integer index;
public CustomClickListener() {
this.index = 0;
}
public void onClick(View v) {
switch (this.index) {
case 0:
//Do wat yo want when index is 0
break;
case 1:
//Do wat yo want when index is 1
break;
case 2:
//Do wat yo want when index is 2
break;
}
}
public void setIndex(Integer index) {
this.index = index;
}
}
然后,在自定义click listener类的活动上实例化一个属性,并将其设置为视图。执行此操作后,如果在任何位置设置此侦听器的索引值,则在单击视图时,索引值将具有正确的值
public class YourActivityClassWithTheViewAndTheIndex {
private View yourView;
private CustomClickListener customClickListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.customClickListener = new CustomClickListener()
this.yourView.setOnClickListener(this.customClickListener);
//iterate over the index, or whatever, and set it to the listener
this.customClickListener.setIndex(3);
}
}
我希望这会有所帮助!在活动中设置一个存储点击次数的字段怎么样?如果您更需要这个字段,您甚至可以使用自己的侦听器实现将其提取出来。将索引变量保留在侦听器之外,确保管理设置和重置此索引的位置,并且每次单击该项目时,都会增加此索引In隐藏onClick方法。在活动中有一个存储单击次数的字段怎么样?如果您更经常需要它,您甚至可以使用自己的侦听器实现将其抽象出来。将索引变量保留在侦听器之外,确保管理设置和重置此索引的位置,并且每次单击该项时,将此索引i递增n使用onClick方法。