Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何通过单击imagebutton设置edittext可动态编辑?_Android_Android Layout_Android Edittext_Android Linearlayout - Fatal编程技术网

Android 如何通过单击imagebutton设置edittext可动态编辑?

Android 如何通过单击imagebutton设置edittext可动态编辑?,android,android-layout,android-edittext,android-linearlayout,Android,Android Layout,Android Edittext,Android Linearlayout,我使用for循环将ArrayList中的字符串值动态列出到EditText中,每个EditText旁边都有ImageButtons。我想要这样:当我点击一个ImageButton时,相应的EditText是可编辑的。 以下是我的活动代码: LinearLayout container = (LinearLayout) findViewById(R.id.container); LayoutInflater layoutInflater = (LayoutInflater) getBaseCont

我使用for循环将ArrayList中的字符串值动态列出到EditText中,每个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);
}  
LinearLayout container=(LinearLayout)findViewById(R.id.container);
LayoutInflater LayoutInflater=(LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
最终视图showProduct=layoutInflater.flate(R.layout.row\u show\u product,null);
对于(int i=0;i
您可以使用相应的编辑文本设置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
                }
            });

我制作了一个快速应用程序来满足您的需求。请看一看

我让代码在一个片段中工作,因此如果您在活动中工作,请进行必要的更改

片段代码:

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();
    }
}
package viewtest.myapplication;
导入android.app.Activity;
导入android.app.Fragment;
导入android.content.Context;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.ViewGroup.LayoutParams;
导入android.view.WindowManager;
导入android.view.inputmethod.InputMethodManager;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ImageButton;
导入android.widget.LinearLayout;
导入android.widget.TextView;
导入java.util.ArrayList;
导入java.util.array;
/**
*包含简单视图的占位符片段。
*/
公共类MainActivityFragment扩展了片段{
公共FragVisibilityInterface mInterface=null;
private LinearLayout showProduct,editTextsLL;
@凌驾
公共事务主任(活动){
超级转速计(活动);
mInterface=新的FragVisibilityInterface(){
@凌驾
公共无效切换FragVisibility(){
}
};
}
公共维护活动片段(){
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment_main,容器,错误);
//Button testBtn=(Button)rootView.findviewbyd(R.id.testbutton);
/*testBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
hideFrag();
}
}); */
字符串[]myArray=getResources().getStringArray(R.array.populate\u数组);
ArrayList产品=新的ArrayList(Arrays.asList(myArray));
对于(int i=0;i
编辑文本和按钮布局

<?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>


代码可能有点粗糙的边缘,因为它是真正的快速。请做相关修改。希望这有帮助!:)

制作图像按钮和编辑文本的ID数组。然后做你已经做过的事情。@andris11m另一种方法是创建和排列列表并添加edittext,同时将edittext的索引设置为imageview的标记,如上所述。