标识特定的动态生成的TextView以便setText

问题描述 投票:0回答:1

我有一个功能,可以动态生成特定数量的TextView。

    TextView tv;
    EditText et;

    public TextView textViewGenerate(final Activity activity, String tag, Integer id) {

        tv = new TextView(activity);

        GradientDrawable gd = new GradientDrawable();
        gd.setColor(0xFFFFFF);
        gd.setCornerRadius(4);
        gd.setStroke(1, 0xFF757575);

        tv.setBackground(gd);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                1.0f
        );

        tv.setPadding(7, 9, 0, 0);
        tv.setGravity(Gravity.START);
        tv.setTextSize(22);

        tv.setTag(tag);
        tv.setId(id);
        tv.setHint("Enter Module Serial Number");

        //Click to launch camera
        tv.setClickable(true);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent intent = new Intent(ct, MctCameraActivity.class);
                Intent intent = new Intent(activity, MctCameraActivity.class);
                activity.startActivityForResult(intent, 1);
                //ct.startActivity(intent);
                //ToDo: finish receiving the data from the activityForResult
            }
        });

        lp.setMargins(10, 0, 10, 0);
        lp.setMarginStart(10);
        lp.setMarginEnd(10);

        tv.setLayoutParams(lp);

        return tv;
    }

}

onClick会启动另一个活动,该活动会加载相机以捕获条形码数据。在主要活动中,我使用

创建TextViews。
        final LayoutElements le = new LayoutElements();
        mainLayout = (LinearLayout)findViewById(R.id.mctScanPageMain);

        for(int i = 0; i < 3; i++) {
            mainLayout.addView(le.textViewGenerate(this, "NewID" + i, i));
        }

onActivityResult返回来自摄像机活动的值,但问题在于,它将每个生成的TextView设置为具有相同的条形码编号(条形码编号作为字符串返回)。如何修改此内容,以便仅在onActivityResult中更改所选TextView的文本?

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && resultCode == RESULT_OK && data != null) {
            String returnResult = data.getStringExtra("result");

            for (int i=0; i < mainLayout.getChildCount(); i++){
                TextView tv = (TextView) mainLayout.findViewWithTag("NewID"+i);
                if(tv == null) {
                    break;
                } else {
                    tv.setText(returnResult);
                }
            }
        }
    }
android android-layout onactivityresult
1个回答
0
投票

在TextView的单击侦听器中,您可以更改:

Intent intent = new Intent(activity, MctCameraActivity.class);
activity.startActivityForResult(intent, 1);

to

Intent intent = new Intent(activity, MctCameraActivity.class);
intent.putExtra("text_view_tag", tag);
activity.startActivityForResult(intent, 1);

然后将其发送回MctCameraActivity的结果,因此可以在onActivityResultgetStringExtra中使用它。这样,您将知道开始活动的TextView标签。

或者,您可以使用传递给requestCodestartActivityForResult来区分文本视图。例如,为文本视图1传递请求代码1,为文本视图2传递请求代码2,等等,然后在requestCode中使用此onActivityResult

© www.soinside.com 2019 - 2024. All rights reserved.