2

Я перечисляю динамически строковые значения из ArrayList в EditTexts с циклом for, а ImageButtons - рядом с каждым EditText. Я хочу это: когда я нажимаю ImageButton, подходящий EditText может быть доступен для редактирования. Вот мой код от деятельности:Как настроить edittext для редактирования динамически, нажав кнопку изображения?

LinearLayout container = (LinearLayout) findViewById(R.id.container); 
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
final View showProduct = layoutInflater.inflate(R.layout.row_show_product, null); 

for (int i = 0; i < product.size(); ++i) { 
    final EditText edt_row_show_product = (EditText) showProduct.findViewById(R.id.edt_row_show_product); 
    edt_row_show_product.setText(product.get(i)); 
    ImageButton ib_row_show_product_edit_icon = (ImageButton)showProduct.findViewById(R.id.ib_row_show_product_edit_icon); 

    ib_row_show_product_edit_icon.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //container.getParent()...; 
     } 
    }); 

    container.addView(showProduct); 
} 
+0

Сделать массив идентификаторов ImageButtons и EditTexts. Затем сделайте то же самое, что и вы сделали. – Android

ответ

1

Вы можете установить установить тег ImageView с соответствующим EditText и получить тег в onClick

final EditText edt_row_show_product = (EditText) showProduct.findViewById(R.id.edt_row_show_product); 

edt_row_show_product.setText(product.get(i));   

ImageButton ib_row_show_product_edit_icon =(ImageButton)showProduct.findViewById(R.id.ib_row_show_product_edit_icon); 

ib_row_show_product_edit_icon.setTag(edt_row_show_product); //set the Edittext object here above 

ib_row_show_product_edit_icon.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        EditText et = (EditText)v.getTag() 
        //do what ever you want with the EditText 
       } 
      }); 
+0

К сожалению, это решение не работает. –

+0

@ andris11m еще один способ - создать и arraylist и добавить edittext, пока вы установите индекс edittext в тег изображения, как и выше. –

2

Я сделал быстрое приложение, чтобы сделать ваше требование работа. Взгляни, пожалуйста.

У меня был код, работающий во Фрагменте, поэтому, если вы работаете в Управлении, внесите необходимые изменения.

фрагмент кода:

package viewtest.myapplication; 

import android.app.Activity; 
import android.app.Fragment; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.ViewGroup.LayoutParams; 
import android.view.WindowManager; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.Arrays; 


/** 
* A placeholder fragment containing a simple view. 
*/ 
public class MainActivityFragment extends Fragment { 

    public FragVisibilityInterface mInterface = null; 

    private LinearLayout showProduct, editTextsLL; 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     mInterface = new FragVisibilityInterface() { 
      @Override 
      public void toggleFragVisibility() { 

      } 
     }; 
    } 

    public MainActivityFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     //Button testBtn = (Button) rootView.findViewById(R.id.testbutton); 

     /*testBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       hideFrag(); 
      } 
     }); */ 
     String [] myArray = getResources().getStringArray(R.array.populate_array); 
     ArrayList<String> product = new ArrayList<>(Arrays.asList(myArray)); 





     for (int i = 0; i < product.size(); ++i){ 
      View showProd = createNewTextView(product.get(i)); 
      LinearLayout editTextLL = (LinearLayout) rootView.findViewById(R.id.editTextsLL); 
      editTextLL.addView(showProd); 
      showProd = null; 
     } 
     return rootView; 
    } 

    /*private void hideFrag() { 
     SecondActivityFragment secFrag = new SecondActivityFragment(); 
     getFragmentManager().beginTransaction().add(R.id.fragment, secFrag, "SecFrag").commit(); 
     getFragmentManager().beginTransaction().hide(this).commit(); 
    }*/ 

    private View createNewTextView(String text) { 
     LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View showProduct = layoutInflater.inflate(R.layout.edit_text_layout, null); 
     final EditText edt_row_show_product = (EditText) showProduct.findViewById(R.id.editText); 
     edt_row_show_product.setText(text); 
     Button ib_row_show_product_edit_icon =(Button) showProduct.findViewById(R.id.button); 

     ib_row_show_product_edit_icon.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       edt_row_show_product.requestFocusFromTouch(); 
       InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.showSoftInput(edt_row_show_product, InputMethodManager.SHOW_IMPLICIT); 

      } 
     }); 
     return showProduct; 
    } 

    public interface FragVisibilityInterface{ 
     public void toggleFragVisibility(); 
    } 
} 

EditText и кнопки Layout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/linearLayout"> 
    <EditText 
     android:id="@+id/editText" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     /> 
    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Edit" 
     /> 
</LinearLayout> 

Главная Фрагмент макета

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment"> 

    <TextView 
     android:id="@+id/textview" 
     android:text="@string/hello_world" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:id="@+id/testbutton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textview" 
     android:text="Button"/> 

    <LinearLayout 
     android:id="@+id/editTextsLL" 
     android:layout_below="@id/testbutton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"></LinearLayout> 

</RelativeLayout> 

Код может быть немного грубо вокруг краев, как это было сделано в реальном быстро. Внесите соответствующие изменения. Надеюсь это поможет! :)

Смежные вопросы