如何检测桌面是否被按下



我知道我可以在每个表格中使用它但是它的代码很长

setOnClickListener(new OnClickListener() {  
    public void onClick(View v)
        {
            //perform action
        }
     });

但是我有12个表行,我认为这是很长…也许用这个结构

tablelayout {
 if (tablerow.id = 1) {
   // action if i pressed the first row
 } else if (tablerow.id = 2) {
   // action if i pressed the second row
 }
}

您可以直接在您的ListView上调用setOnItemClickListener。因此,不要在适配器的getView方法中调用setOnClickListener,只需使用以下命令:

ListView mListView = (ListView) findViewById(R.id.listview);//or whatever the id is
mListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //then use a switch statement
        switch(position) {
            case 1 : 
                //do something if first row is clicked
                break;
            case 2 : 
                //row 2 clicked
                break;//don't forget these breaks
        }
    }
});

您可以使用View.setTag(Object obj):

OnClickListener mListener = new OnClickListener() {  
    public void onClick(View v) {
        Integer tag = (Integer)v.getTag();
        switch (tag.intValue()) {
            case ROW1: ...
        }
    }
 });

和时间你填充你的行只是做(可能在所有行-id的循环):

TableRow row = tableLayout.findViewById(ROW1);
row.setTag(ROW1);
row.setOnClickListener(mListener);

顺便说一下,标签也可以在XML中设置…

如果你使用的是一个TableLayout,你可以添加单独的OnClickListener到你的TableRow(在你的TableLayout)内部的元素。

例如,如果你有一个按钮和一个TextView在TableRow中,你想让你的按钮在点击时做一些事情,那么你应该添加OnClickListener到按钮。如果所有的按钮都到达相同的位置,但是根据在TableLayout中的位置发送不同的数据那么你可以这样做:

String[] texts; //The different texts your button could say
for(int i = 0; i < 12; i++){
  final Button yourButton = new Button(this);
  yourButton.setText(texts[i]);
  yourButton.setOnClickListener(new OnClickListener(){
    Intent i = new Intent(this, YourClass.class);
    i.putExtra(yourButton.getText());
    startActivity(i);
  }
}

如果你的tableow只包含一个元素,你应该使用ListView而不是像Phil建议的那样使用TableLayout。

我希望这能给你一个方向的想法,但这实际上取决于你需要对被点击的项目做什么

  /************ if table row is dynamic then this method else method 2 is perfect************/
//if we want to applay listener on dynamic tablerow then use this
//sure that perfect 
 TablRowe tr = new TableRow(this);
tr.setClickable(true);
tr.setId(100);// if in loop then add 1 counter with 100 like (100+counter) at end count++ it
 tr.setOnClickListener(this);
 @Override
    public void onClick(View v) 
{
        switch (v.getId())
         {
         case 100: 
               Toast.makeText(getApplicationContext(), "100", Toast.LENGTH_SHORT).show();   
             break;
         case 101:
            Toast.makeText(getApplicationContext(), "101", Toast.LENGTH_SHORT).show();  
             break;
}
/************************** for simple like this ************************/
   TableRow row1 = (TableRow)findViewById(R.id.row1);
row1.setonClickListener(this);
public void onClick(View v)
{
switch (v.getId())
         {
         case R.id.row1: 
           // do work
             break;
          }        
}

最新更新