我使用for循环将ArrayList中的字符串值动态列入EditTexts,并且每个EditText旁边都有ImageButtons。我想要这个:当我单击一个ImageButton时,相应的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 :(得分:2)
我制作了一个快速的应用程序,以使您的需求工作。请看一下。
我让代码在Fragment中工作,所以如果您正在使用Activity,请进行必要的更改。
片段代码:
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和按钮布局
<?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>
代码可能在边缘有点粗糙,因为它真的很快。请进行相关更改。希望这可以帮助! :)
答案 1 :(得分:1)
您可以使用相应的EditText设置imageview的标记,并在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
}
});