How to setup onFocusChangeListener() on a dynamically created set of editTexts?

两盒软妹~` 提交于 2019-12-23 21:43:51

问题


i have this code in which i inflate a linearLayout containing 3 editTexts everytime an edittext of the previous lineaLayout loses focus. I want to have the onFocusChangeListener on the most recent created editTexts only. Instead, onFocusChangeListener is called only when the the first linearLayout loses focus and not the rest of others.

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

I created an array of boolean to know whether the last editText had created a new linearLayout(listened to the onFocusChangeListener). help!!!


回答1:


You need to extend the EditText class so you create your own type of View where might want to have an inner class which will implement View.OnFocusChangeListener and public method so you can setup listener with an instance of this inner class using the instance of newly created type.



来源:https://stackoverflow.com/questions/18258875/how-to-setup-onfocuschangelistener-on-a-dynamically-created-set-of-edittexts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!