简体   繁体   中英

how to set up a setOnClickListener on a GroupView of EditText

I would like to insert my array of EditText, contained in a LinearLayout, in a ViewGroup so as to be able to set for all EditText the same setonclicklistener. How do I proceed?

This is the code for my attempt:

public class Main extends Activity implements  OnClickListener {

ViewGroup group;;
View v1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

group = (ViewGroup)findViewById(R.id.xsubLayout);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
v1 = group.getChildAt(i);
if (v1 instanceof EditText) {
((EditText)v1).setOnClickListener(this);
}
}

and OnClick method:

@Override
public void onClick(View v) {

if(v == v1 ){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v1.getWindowToken(), 0);


enableKeyboard();
}

}

but this way is not working. In practice Continues to see the Android keyboard instead of my custom keyboard

You have to implement onClickListener() for your class. For example like this.

  public class Main extends Activity implements  OnClickListener {

     EditText etText;

     @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     etText = (EditText)findViewById(R.id.yourEditTextId);
     btnRemove.setOnClickListener(this)
    }

public void onClick(View view) {
    // TODO Auto-generated method stub
   // Here you can perform same task for all the EditText.

}
  }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM