用于在Android中以编程方式创建的多个textview的单个onclick监听器

时间:2017-10-12 11:30:49

标签: android view onclicklistener textview

所以我使用下面编写的代码以编程方式成功创建了许多textview。我想为这些textview创建一个高效的单一监听器,因为我需要在click上执行一个动作。我如何知道哪个文本视图被点击并以编程方式附加一个监听器?

PS:请不要使用XML布局文件发布解决方案。提前致谢。

optionCubesTextviews = new TextView[cubesLength];
    for(i=0;i<cubesLength;i++) {
        optionCubesTextviews[i] = new TextView(this);
        optionCubesTextviews[i].setId(View.generateViewId());
        optionCubesTextviews[i].setTextSize(baseFont);
        final RelativeLayout.LayoutParams optionCubesTextviewsParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        optionCubesTextviewsParams.addRule(RelativeLayout.BELOW, solutionCubeTextviews[i].getId());
        if (i != 0) {
            optionCubesTextviewsParams.addRule(RelativeLayout.RIGHT_OF, optionCubesTextviews[i - 1].getId());
        }
        optionCubesTextviewsParams.setMargins(10, 10, 10, 10);
        optionCubesTextviews[i].setPadding(20, 20, 20, 20);
        optionCubesTextviews[i].setTypeface(Typeface.DEFAULT);
        //String qTextColorColor = preferenceFile.getString("qTextColor", DefaultManager.qTextColor);
        optionCubesTextviews[i].setTextColor(Color.WHITE);
        optionCubesTextviews[i].setBackgroundColor(Color.parseColor(randomColorString));
        optionCubesTextviews[i].setText(Character.toString(shuffledAnswerAsOptions.charAt(i)));
        optionCubesTextviews[i].setVisibility(View.VISIBLE);

        //Adding to Layout in invisible state
        rLayout.addView(optionCubesTextviews[i], optionCubesTextviewsParams);
    }

2 个答案:

答案 0 :(得分:2)

只需创建一个侦听器并将其分配给您的视图,就像任何其他属性一样。

OnClickListener l = new OnClickListener(){
    public void onClick(View v){
        // TODO whatever...
    }
}

for(i=0 ; i < optionCubesTextviews.length; i++) {
    optionCubesTextviews[i].setTag(i);
    optionCubesTextviews[i].setOnClickListener(l);
}

答案 1 :(得分:0)

检查我做的这个例子,我希望它可以帮助你:)

通过这种方式,您可以查看点击的视图。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  // THIS IS MY MAIN LAYOUT
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main_layout);

    for (int i = 0; i < 3; i++) {
        TextView textView = new TextView(this);
        textView.setTag(String.valueOf(i));
        textView.setOnClickListener(this);
        textView.setText("TEXT VIEW " + i);
        linearLayout.addView(textView);
    }
}

@Override
public void onClick(View view) {
    switch (view.getTag().toString()) {
        case "0":
            Toast.makeText(this, "TEXT VIEW 1", Toast.LENGTH_LONG).show();
            break;
        case "1":
            Toast.makeText(this, "TEXT VIEW 2", Toast.LENGTH_LONG).show();
            break;
        case "2":
            Toast.makeText(this, "TEXT VIEW 3", Toast.LENGTH_LONG).show();
            break;
        default:
            Toast.makeText(this, "Default", Toast.LENGTH_LONG).show();
    }
}}