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

2024-05-07

我有这段代码,每次前一个 lineaLayout 的 edittext 失去焦点时,我都会膨胀一个包含 3 个 editText 的 LinearLayout 。我只想在最近创建的 editTexts 上使用 onFocusChangeListener 。相反,仅当第一个 LinearLayout 失去焦点时才会调用 onFocusChangeListener,而其他线性布局则不会。

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());
        }
    }
}

我创建了一个布尔数组来了解最后一个editText是否创建了一个新的线性布局(监听了onFocusChangeListener)。帮助!!!


您需要延长EditText类,以便您创建自己的类型View哪里可能想要一个内部类来实现View.OnFocusChangeListener http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html and public方法,这样您就可以使用新创建类型的实例来设置此内部类的实例的侦听器。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在动态创建的一组 editText 上设置 onFocusChangeListener()? 的相关文章

随机推荐