gpt4 book ai didi

android - 如何在动态创建的一组 editText 上设置 onFocusChangeListener()?

转载 作者:太空狗 更新时间:2023-10-29 15:09:26 25 4
gpt4 key购买 nike

我有这段代码,其中每次前一个 lineaLayout 的编辑文本失去焦点时,我都会膨胀一个包含 3 个 editText 的 linearLayout。我只想在最近创建的 editTexts 上使用 onFocusChangeListener。相反,onFocusChangeListener 仅在第一个 linearLayout 失去焦点时被调用,而其他的则不会。

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sale_purchase_vouch);
no=0;
for(int i=0;i<30;i++)
flag[i]=true;

save=(Button)findViewById(R.id.Save);
save.setText("Confirm Purchase");
LayoutInflater l=getLayoutInflater();
container=(LinearLayout)findViewById(R.id.container);
row[no]=(LinearLayout)l.inflate(R.layout.row, container);
items[no]=(AutoCompleteTextView)row[no].findViewById(R.id.item);
quants[no]=(EditText)row[no].findViewById(R.id.quant);
rates[no]=(EditText)row[no].findViewById(R.id.rate);

quants[no].setOnFocusChangeListener(this);
flag[no]=false;


}


@Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub
if(flag[no+1]==true){

if(arg1==false){
no++;
LayoutInflater g=getLayoutInflater();

row[no]=(LinearLayout)g.inflate(R.layout.row, container);
items[no]=(AutoCompleteTextView)row[no].findViewById(R.id.item);
quants[no]=(EditText)row[no].findViewById(R.id.quant);
rates[no]=(EditText)row[no].findViewById(R.id.rate);
Log.d("detection", "Row is "+ no+ arg0.getId());
}
}
}

我创建了一个 bool 值数组来了解最后一个 editText 是否创建了一个新的 linearLayout(监听了 onFocusChangeListener)。帮助!!!

最佳答案

您需要扩展 EditText 类,以便创建您自己的 View 类型,其中可能需要一个将实现 View.OnFocusChangeListener 的内部类和 public 方法,因此您可以使用新创建类型的实例使用此内部类的实例设置监听器。

关于android - 如何在动态创建的一组 editText 上设置 onFocusChangeListener()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18258875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com