单击Android上动态添加的TextViews上的事件

时间:2012-12-20 10:17:46

标签: android textview onclicklistener

我正在尝试添加20个文本视图,并为动态添加的文本视图分配onclick。问题是每当我尝试点击任何动态textiview时。它总是触发上次添加的textview的点击事件。

这是我的代码:

EditText s;
EditText t;

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

    RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container);

    for (int i = 0; i < 20; i++) {
        TextView dynaText = new TextView(this);

        dynaText.setText("Some text " + i);
        dynaText.setTextSize(30);
        dynaText.setTag("" + i);

        dynaText.setOnClickListener(btnClickListener);
        // Set the location of your textView.

        dynaText.setPadding(0, (i * 30), 0, 0);

        containerLayout.addView(dynaText);
    }

}

OnClickListener btnClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        RelativeLayout ll = (RelativeLayout) v.getParent();
        TextView tv = (TextView) ll.getChildAt(2);
        // Integer pos = (Integer) tv.getTag();
        Toast.makeText(v.getContext(), "Toast " + v.getTag(),
                Toast.LENGTH_SHORT).show();
    }

};

}

4 个答案:

答案 0 :(得分:3)

1)您应首先制作TextViews 可点击

for (int i = 0; i < 20; i++) {
        TextView dynaText = new TextView(this);

        dynaText.setText("Some text " + i);
        dynaText.setTextSize(30);
        dynaText.setTag("" + i);
        dynaText.setClickable(true);//make your TextView Clickable
        dynaText.setOnClickListener(btnClickListener);
        // Set the location of your textView.

        dynaText.setPadding(0, (i * 30), 0, 0);

        containerLayout.addView(dynaText);
    }

2)修改您的onClickListener

OnClickListener btnClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
          Log.d("btnClickListener", ""-----TextView Clicked : "+v.getTag());
          Toast.makeText(YourActivity.this, "TextView Clicked : "+v.getTag(),
                Toast.LENGTH_SHORT).show();
    }

};

编辑:您正在添加TextViews一个。这就是为什么click事件会在最后添加的事件中触发。 尝试将LinearLayoutandroid:orientation="vertical"一起使用,或者尝试添加前一个新的TextView:

for (int i = 0; i < 20; i++) {
        TextView dynaText = new TextView(this);

        dynaText.setText("Some text " + i);
        dynaText.setId(i+1);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        if(i!=0)
         params.addRule(RelativeLayout.BELOW, i);
         dynaText.setLayoutParams(params);
        dynaText.setTextSize(30);
        dynaText.setTag("" + i);

        dynaText.setOnClickListener(btnClickListener);

        containerLayout.addView(dynaText);
    }

答案 1 :(得分:2)

试试这个:

OnClickListener btnClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
          Toast.makeText(v.getContext(), "Toast " + v.getTag(),
                Toast.LENGTH_SHORT).show();
    }

};

<强>更新

你点击最后一个TextView的原因是因为它太大了..你正在增加每个循环的填充,当你点击某个地方时,它总是会击中最后一个View因为它覆盖了其他人。尝试LinearLayout并删除填充,如下所示:

public class MainActivity extends Activity implements OnClickListener {

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

        LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container);

        for (int i = 0; i < 20; i++) {
            TextView dynaText = new TextView(this);

            dynaText.setText("Some text " + i);
            dynaText.setTextSize(30);
            dynaText.setTag("" + i);
            dynaText.setOnClickListener(this);

            containerLayout.addView(dynaText);
        }
    }

    public void onClick(View v) {
        // do stuff
    }

}

但是你的问题已经解决了......我认为ListView符合你的目的:)

答案 2 :(得分:1)

以下列方式获取点击数据

声明变量

int sCount = 0;
for (int i = 0; i < 20; i++) {
        TextView dynaText = new TextView(this);
        dynaText.setId(1000+sCount);
        dynaText.setText("Some text " + i);
        dynaText.setTextSize(30);
        dynaText.setTag("" + i);

        dynaText.setOnClickListener(btnClickListener);
        // Set the location of your textView.

        dynaText.setPadding(0, (i * 30), 0, 0);

        containerLayout.addView(dynaText);
    }
OnClickListener btnClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        RelativeLayout ll = (RelativeLayout) v.getParent();
        TextView tv = (TextView) ll.getChildAt(2);
        tv.addFocusables(null, v.getId(), 0);
        Toast.makeText(v.getContext(), "Toast " + tv.getText(),
                Toast.LENGTH_SHORT).show();
    }

};

这适合你。

答案 3 :(得分:0)

您可以尝试为OnClickListener使用匿名实例,并为每个视图创建不同的侦听器实例。像这样:

dynaText.setPadding(0, (i * 30), 0, 0);
dynaText.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    ((TextView) v).getText();

}
});
containerLayout.addView(dynaText);