如何在for循环中使用接口

huangapple go评论57阅读模式
英文:

How can I use interface from a for loop

问题

我在MainActivity类中有两个名为CalculatorClassUpdatePayment内部类
UpdatePayment类中有一个for循环,我有一个按钮数组
我想在循环中为每个按钮添加监听器。这些按钮将初始化CalculatorClass并获取计算值。

演示代码如下:

public static class MainActivity {

    private interface UpdateEditText {
        void onCallback(String s);
    }

    private class CalculatorClass extends Dialog {
        UpdateEditText updateEditText;

        public CalculatorClass(@NonNull Context context, UpdateEditText updateEditText) {
            super(context);
            this.updateEditText = updateEditText;
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.calculator);
            initialize();
        }

        private void initialize() {
            // .............
            String s = "一些文本";
            updateEditText.onCallback(s);
        }
    }

    private class UpdatePayment extends Dialog {

        private Button[] button = new Button[100];
        private EditText[] editText = new EditText[100];

        public UpdatePayment(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.update);
            initialize();
        }

        private void initialize() {
            // .............
            for (int i = 0; i < MAXSize; i++) {
                button[i] = new Button(MainActivity.this);
                editText[i] = new EditText(MainActivity.this);
                // 将按钮添加到布局视图
                button[i].setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        final int index = i;
                        CalculatorClass calculator = new CalculatorClass(MainActivity.this,
                                new UpdateEditText() {
                                    @Override
                                    public void onCallback(String s) {
                                        editText[index - 1].setText(s);
                                    }
                                });
                        calculator.show();
                    }

                });
            }
        }
    }
}

问题是editText[i].setText(s)这行代码对于无论我点击哪个按钮,都只对最后一个editText起作用,即,无论我点击哪个按钮,它都填充到editText[MaxSize -1]

我应该怎么做?
如何为所有的i执行此操作?
请帮帮我,我在互联网上尝试了很多搜索,但仍然没有找到解决方案。

英文:

I have two inner classes named CalculatorClass and UpdatePayment in MainActivity class.
In UpdatePayment class there is a for loop and I have a array of Buttons.
I want to add listener to each button in loop. Those buttons will initialize the CalculatorClass and get value of calculations.

Demo code is:

public static class MainActivity{
private interface UpdateEditText{
void onCallback(String s);
}
private class CalculatorClass extends Dialog{
UpdateEditText updateEditText;
public CalculatorInterface(@NonNull Context context, UpdateEditText updateEditText) {
super(context);
this.updateEditText = updateEditText;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
initialize();
}
initialize(){ 
.......................
s = &quot;Some texts&quot;;
updateEditText.onCallback(s);
}
}
private class UpdatePayment extends Dialog{
private Button[] button = new Button[100];
private EditText[] editText = new EditText[100];
public CalculatorInterface(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
initialize();
}
initialize(){ 
.......................
for(int i = 0; i &lt; MAXSize; i++){
button[i] = new Button(MainActivity.this);
editText[i] = new EditText(MainActivity.this);
//add buttons to view to layout
button[i].setOnclickListener(new View.OnClickListener(){
public void onClick(View v) {
CalculatorClass calculator = new 
CalculatorClass(MainActivity.this,
new UpdateEditText() {
@Override
public void onCallback(String s) {
editText[i - 1].setText(s);
}
});
calculator.show();
}
);
}
}
}
}

Problem is the line editText[i].setText(s) work for the last editText what ever button I click, i.e, any button I click, it fills the editText[MaxSize -1]

What should I do?
How can I perform this action for all i?
Please help me, I tried a lot searching in internet, still I didn't get any solution.

答案1

得分: 0

匿名类在Java中很类似于静态变量。这是因为在初始化活动后,您的i值等于编辑文本数组的长度减1。

editText[indicies_of_button_clicked].setText(s);

要获取按钮点击的索引,可以这样做:
在此行下面为按钮设置标签,就像这样:

button[i].setTag(i);

要在按钮点击侦听器内获取实际点击的按钮索引,

v.getTag(); // 这将为您提供一个整数值,该值将是实际按钮点击的索引
           // 使用此值设置您的编辑文本值。
英文:

Anonymous classes are very much treated like static variables in java.It's happenning because your i value after your activity is initialized is equal to 1 less than the length of the edit text array.
editText[indicies_of_button_clicke].setText(s)

How you will get the indicies of button clicked is by :
se the tag to button below this line "editText[i] = new EditText(MainActivity.this);", like this :
button[i].setTag(i)

to retreive the index of your actual button clicked inside button clicklisteners,

  v.getTag()// this will give you an integer value which will be the indices of actual button clicked
//use this value to set your edit text value.

答案2

得分: 0

解决方案:

经过一个整晚,我得到了答案,现在我感觉,噢,这很简单!我从未想过这一点。

button[i].setId(i);

然后调用:

CalculatorClass calculator = new CalculatorClass(MainActivity.this, v.getId(),
                            new UpdateEditText() {
                                @Override
                                public void onUpdate(String s, int ID) {
                                    Log.i("test", ID + "");
                                    editText[ID].setText(s);
                                }
                            });

同时,@Rishabh Ritweek 的回答也是正确的。
感谢他。

英文:

Solution:

After a whole night, I got answer, now I feel awwww, this was easy go!
I never came in this thought.

button[i].setId(i);

then call:

CalculatorClass calculator = new CalculatorClass(MainActivity.this, v.getId(),
new UpdateEditText() {
@Override
public void onUpdate(String s, int ID) {
Log.i(&quot;test&quot;, ID + &quot;&quot;);
editText[ID].setText(s);
}
});

At the same time, @Rishabh Ritweek answered correctly.
Thanks to him.

huangapple
  • 本文由 发表于 2020年10月9日 06:17:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64271407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定