Android - setting a text field when a button is clicked

I have a gridview and each element has a text view. I would like SetText to display text when I click an element in a gridview.

its method: setOnItemClickListener

but I don’t know how to control the text view.

any ideas, examples, or tutorials? thanks.

+4
source share
4 answers

You can inflate a text image from a layout file into a TextView instance. Then just set the text for it, as shown below:

 TextView text = (TextView) findViewById(R.id.this_is_the_id_of_textview); text.setText("test"); 
+3
source

You can handle the click event TextView inside the getView method. If he clicks, just change the text.

Edit

 ...getView(...) { TextView text = (TextView) convertView.findViewById(R.id.this_is_the_id_of_textview); text.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TextView was clicked! // Change the text } }); 

}

+3
source

Just try this one.

 public void onItemClick(AdapterView<?> adapView, View view, int position, long arg3) { TextView textview=(TextView)view.findViewById(R.id.id_of_textview); textview.setText("write your text here"); } 
+2
source

let's say you have 4 grids:

 String[] subtitle = {"Hotel", "Store","Restaurant","Station"}; 

set onItemClick for gridview:

 GridView gView = (GridView) findViewById(R.id.gridview); gView.setAdapter(new ImageAdapter(this)); gView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView ket =(TextView)findViewById(R.id.test); ket.setText(subtitle [position]); } }); 

therefore, if you click / onclick in the image grid, the result will display an array row based on position.

-1
source

Source: https://habr.com/ru/post/1440446/


All Articles