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

标签 android dynamic runtime android-linearlayout

我有这段代码,其中每次前一个 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/

相关文章:

Python 在 chroot 中运行时出现错误

Android SQLite 数据库损坏

java - 如何在android中格式化longs以始终显示两位数

php - 如何使用 PHP 创建动态页面标题

google-app-engine - 动态后端如何在 Google App Engine 中启动

scala - Scala系统进程挂起

c++ - 使用 pthread 计算平均运行时间,结果很奇怪

android - 如何检查可观察对象中的空数组?

android - 为扩展 Activity 或 Fragment 的所有类编写通用方法的设计模式

PostgreSQL 交叉表查询从辅助查询中提取列