一般来说是android和编程的新手,所以学习我的概念.
我有一个LinearLayout,我动态添加edittexts.
我需要能够在选择或焦点时获取任何edittext的索引和ID.
我已经尝试循环子计数以检查选择如下:
int count = llmain.getChildCount();
for (int i=0; i< count; ++i) {
if ((llmain.getChildAt(i).isSelected()) == true){
//Do Stuff
}
Run Code Online (Sandbox Code Playgroud)
但我不知道它是否接近,这只是索引......
非常感谢帮助!
谢谢
编辑:仍然没有一个可靠的方法来实现这一目标.下面的例子用
if(v instanceOf EditText) {
id = v.getId();
index = ll.indexOfChild(v);
Run Code Online (Sandbox Code Playgroud)
为索引返回-1,为id返回10位数,但是,我在创建时分配id.?奇怪的是"if"中的代码正在运行,所以它至少认为它具有焦点视图.现在,如果我将"instanceof"更改为null检查,就像
if (v != null){
id = v.getId();
index = llmain.indexOfChild(v);
Run Code Online (Sandbox Code Playgroud)
我添加了一个setFocusableInTouchMode(true),我得到一个正确的返回,然而,它然后就像我调用clearFocus(),因为没有一个EditTexts被聚焦.这是我的概念代码的完整证明,它返回正确的值,但不再让EditTexts实际上具有焦点.
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&(event.getKeyCode() == 66)) // KeyEvent.* lists all the key codes u pressed
{
View myView = linflater.inflate(R.layout.action, null);
myView.setId(pos);
pos++;
myView.setFocusableInTouchMode(true);
llmain.addView(myView); …Run Code Online (Sandbox Code Playgroud) android ×1