从选定的EditText android获取getId

时间:2016-10-15 20:28:54

标签: android android-edittext onclicklistener selecteditem

这是我的第一个问题,我已经创建了一个代码来添加SQLite数据库中的视图,我想选择一个我添加的EditText,当我更改此值时,请执行操作。我不知道这个EditText的ID,所以我不能在这种情况下使用findByValue。我怎么能得到这个?这是我的代码:

LinearLayout pantalla;
int[] edits;
int adicionales = 0;
int IdSelected; //I want to put the Id here

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_platos);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    edits = getResources().getIntArray(R.array.editTexts); //these are the Id values that I've created on String file
    pantalla = (LinearLayout) findViewById(R.id.pantallaPlatos);

    CargarPlatos(); //this calls to create all the EditTexts

}

public void CargarPlatos() {
    pantalla.removeAllViews();

    List<tablaPlatos> Platos = Consumo.db.getAllPlatos();
    int i = 0;
    for (tablaPlatos platos : Platos) {
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        pantalla.addView(linearLayout);

        EditText item = new EditText(this);
        String log = platos.getName();
        item.setText(log);
        item.setId(edits[i]);
        ;
        i++;
        linearLayout.addView(item);

        EditText item2 = new EditText(this);
        String log2 = platos.getValue();
        item2.setText(log2);
        item2.setId(edits[i]);
        item2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        i++;
        linearLayout.addView(item2);
    }

}

// When I click on an EditText I want to know his Id
@Override
public void onClick(View v) {
    IdSelected=v.getId();
    //And then do something
}

ScreenCapture of this code

真的希望你能帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

好的,谢谢你试图帮助我,我用OnGlobalFocusChangeListener找到了我的解决方案

我在OnCreate上添加了这段代码:

block title

有了这个,我可以看到专注的EditText的ID