有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将onClickListeners设置为多个视图

我有很多视图(大约30个,可能会更多),比如卡片。 我为每一个设置了onClickListener,但它不起作用。 没有反应。 这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
    final EditText searchProfile = (EditText) findViewById(R.id.search_profile);
    final Button btnSearch = (Button) findViewById(R.id.btn_search);
    final LinearLayout layoutProfileLeft = (LinearLayout) findViewById(R.id.layout_profiles_left);
    searchProfile.setHint("UserName...");
    btnSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String profileName = searchProfile.getText().toString();
            urlForProfile = "http://www.dotabuff.com/search?utf8=&q=" + searchProfile.getText().toString().replace(" ", "+") + "&commit=Search";
            while (htmlSrc == "" || htmlSrc == null) {
                setHtmlSrc(urlForProfile);
            }
            ArrayList<Integer> ids = new ArrayList<Integer>();
            for (int i = 0; i < 30; i++) {
                searchedHeroes.add(i, new SearchedHero(i));
                RelativeLayout l = new RelativeLayout(StartActivity.this);
                RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(-1,-2);
                rlp.setMargins(8, 0, 8, 8);
                l.setLayoutParams(rlp);
                l.setBackgroundColor(Color.rgb(69,90,100));
                l.setPadding(8,0,8,8);
                l.setId(i);
                //l.setLayoutParams(LinearLayout.);
                ImageView img = new ImageView(StartActivity.this);
                new DownloadImageTask(img).execute(searchedHeroes.get(i).getImageSrc());
                img.setOnClickListener(this);
                img.setId(i + 40);
                l.addView(img);
                RelativeLayout innerL = new RelativeLayout(StartActivity.this);
                RelativeLayout.LayoutParams paramsForInnerL = new RelativeLayout.LayoutParams
                        (-2, -2);
                paramsForInnerL.addRule(RelativeLayout.RIGHT_OF, img.getId());
                TextView txtProfileName = new TextView(StartActivity.this);
                txtProfileName.setText(searchedHeroes.get(i).getName());
                txtProfileName.setId(i + 80);
                TextView txtLastMatch = new TextView(StartActivity.this);
                txtLastMatch.setText(searchedHeroes.get(i).getLastMatch());
                txtProfileName.setTextSize(14);
                txtProfileName.setTextColor(Color.WHITE);
                txtLastMatch.setId(i + 120);
                RelativeLayout.LayoutParams paramsForNameLastMatch = new RelativeLayout.LayoutParams
                        (
                                RelativeLayout.LayoutParams.WRAP_CONTENT,
                                RelativeLayout.LayoutParams.WRAP_CONTENT
                        );
                innerL.addView(txtProfileName, rlp);
                paramsForNameLastMatch.addRule(RelativeLayout.BELOW, txtLastMatch.getId());
                innerL.addView(txtLastMatch, paramsForNameLastMatch);
                l.addView(innerL, paramsForInnerL);
                l.setOnClickListener(this);
                ids.add(i, l.getId());
                layoutProfileLeft.addView(l, rlp);
                //}
                //m.find();
                //passUrl = "http://www.dotabuff.com" + m.group(1);
                //Intent pass = new Intent(StartActivity.this, MainActivity.class);
                //pass.putExtra("EmpID", searchedHeroes.get(0).getProfileUrl());
                //startActivity(pass);
                //}
                //}
            }
        }
    });
}

@Override
public void onClick(View v) {
    Intent pass = new Intent(StartActivity.this, MainActivity.class);
    pass.putExtra("Link", searchedHeroes.get(v.getId()).getProfileUrl());
    startActivity(pass);
}

也许,每个视图都必须包含其他活动。 谢谢


共 (2) 个答案

  1. # 1 楼答案

    在我看来,您似乎忘记了将视图设置为可点击: view.setClickable(true)

  2. # 2 楼答案

    public class MainActivity extends Activity implements View.OnClickListener {
    EditText searchProfile;
    Button btnSearch;
    LinearLayout layoutProfileLeft
    
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
    searchProfile = (EditText) findViewById(R.id.search_profile);
    btnSearch = (Button) findViewById(R.id.btn_search);
    layoutProfileLeft = (LinearLayout) findViewById(R.id.layout_profiles_left);
    searchProfile.setHint("UserName...");
    btnSearch.setOnClickListener(this); 
    
    }
    
    @Override
    public void onClick(View v) {
    Intent pass = new Intent(StartActivity.this, MainActivity.class);
     String profileName = searchProfile.getText().toString();
            urlForProfile = "http://www.dotabuff.com/search?utf8=&q=" + searchProfile.getText().toString().replace(" ", "+") + "&commit=Search";
            while (htmlSrc == "" || htmlSrc == null) {
                setHtmlSrc(urlForProfile);
            }
            ArrayList<Integer> ids = new ArrayList<Integer>();
            for (int i = 0; i < 30; i++) {
                searchedHeroes.add(i, new SearchedHero(i));
                RelativeLayout l = new RelativeLayout(StartActivity.this);
                RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(-1,-2);
                rlp.setMargins(8, 0, 8, 8);
                l.setLayoutParams(rlp);
                l.setBackgroundColor(Color.rgb(69,90,100));
                l.setPadding(8,0,8,8);
                l.setId(i);
                //l.setLayoutParams(LinearLayout.);
                ImageView img = new ImageView(StartActivity.this);
                new DownloadImageTask(img).execute(searchedHeroes.get(i).getImageSrc());
                img.setOnClickListener(this);
                img.setId(i + 40);
                l.addView(img);
                RelativeLayout innerL = new RelativeLayout(StartActivity.this);
                RelativeLayout.LayoutParams paramsForInnerL = new RelativeLayout.LayoutParams
                        (-2, -2);
                paramsForInnerL.addRule(RelativeLayout.RIGHT_OF, img.getId());
                TextView txtProfileName = new TextView(StartActivity.this);
                txtProfileName.setText(searchedHeroes.get(i).getName());
                txtProfileName.setId(i + 80);
                TextView txtLastMatch = new TextView(StartActivity.this);
                txtLastMatch.setText(searchedHeroes.get(i).getLastMatch());
                txtProfileName.setTextSize(14);
                txtProfileName.setTextColor(Color.WHITE);
                txtLastMatch.setId(i + 120);
                RelativeLayout.LayoutParams paramsForNameLastMatch = new RelativeLayout.LayoutParams
                        (
                                RelativeLayout.LayoutParams.WRAP_CONTENT,
                                RelativeLayout.LayoutParams.WRAP_CONTENT
                        );
                innerL.addView(txtProfileName, rlp);
                paramsForNameLastMatch.addRule(RelativeLayout.BELOW, txtLastMatch.getId());
                innerL.addView(txtLastMatch, paramsForNameLastMatch);
                l.addView(innerL, paramsForInnerL);
                l.setOnClickListener(this);
                ids.add(i, l.getId());
                layoutProfileLeft.addView(l, rlp);
            }
    pass.putExtra("Link", searchedHeroes.get(v.getId()).getProfileUrl());
    startActivity(pass);
    }