简体   繁体   English

Android Studio TextViews onClick都执行相同的操作,如何解决?

[英]Android Studio TextViews onClick all perform the same action, how do I fix it?

I have this function which is supposed to create an array of TextViews with unique ids. 我有这个功能,应该创建一个具有唯一ID的TextViews数组。

Each TextView is supposed to perform a unique action, however when any one of them is clicked, they perform the function of the last TextView . 每个TextView应该执行唯一的操作,但是,当单击其中任何一个时,它们都将执行最后一个TextView的功能。

(ie, anyone of them appends a 9 to the last TextView the way this i set up) Do you know why it does this, and how I can fix it? (即,他们中的任何人都以我设置的方式将9附加到最后一个TextView上)您知道为什么这样做以及如何解决它吗?

Any help would be greatly appreciated. 任何帮助将不胜感激。

//Code:   
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_what_can_imake);

        int textViewCount = 10;

        TextView[] textViewArray = new TextView[textViewCount];
        RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myLayout);
        for(int i = 0; i < textViewCount; i++) {
            textViewArray[i] = new TextView(this);
            textViewArray[i].setText("Title"+Integer.toString(i));
            textViewArray[i].setPadding(8,8+50*i,8,0);
            textViewArray[i].setId(i);
            LayoutParams myTitleDimensions = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);


            textViewArray[i].setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int myId = v.getId();
                    ((TextView) v).append(Integer.toString(myId));
                }
            });
            myLayout.addView(textViewArray[i],myTitleDimensions);
        }

    }

You are using different paddingTop to layout your TextView s vertically: 您正在使用其他paddingTop垂直布置TextView

textViewArray[i].setPadding(8,8+50*i,8,0);

this makes the TextView s visually separate to each other, but in fact they are all overlapped, the 2nd one overlapped the 1st, the 3rd one overlapped the 2nd, etc. At last, the 9th one overlapped all, so no matter which text you clicked, you actually clicked the 9th one. 这使TextView在视觉上彼此分离,但是实际上它们都是重叠的,第二个重叠了第一个,第三个重叠了第二个,依此类推。最后,第9个重叠了所有,所以无论您使用哪种文本单击,您实际上单击了第九个。

To fix this, you should change the way you layout the TextView s. 要解决此问题,您应该更改TextView的布局方式。

For example, use RelativeLayout.addRule(int verb, int anchor) : 例如,使用RelativeLayout.addRule(int verb, int anchor)

RelativeLayout.LayoutParams myTitleDimensions = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
    myTitleDimensions.addRule(RelativeLayout.BELOW, i - 1);
}

By the way, 0 is not a valid id, so your 1st TextView will be still overlapped by the 2nd one, just change the way to generate ids a little. 顺便说一句, 0不是有效的ID,因此您的第一个TextView仍将与第二个TextView重叠,只需稍微更改生成ID的方式即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM