2013-03-21 68 views
3

定义在下面的活动中,我创建一个表,并动态地添加按钮。这些按钮没有在xml中定义,我想知道在哪个位置点击了按钮(标记为'b')。即:其中我和j 我尝试将jClickable设置为i,但它将它设置为最终值,因为它在创建表后单击。Android:确定哪个按钮被点击给定该按钮没有在xml

aFromA,aToA,mobileA,LnameA,FnameA,fullName,aIDa都是字符串数组,我已经删除了它们的初始化部分,因为它由于拆分和其他操作需要一大块代码。

任何帮助将不胜感激,在此先感谢。下面的代码:

public class ViewAssistants extends Activity implements OnClickListener{ 


    Button back; 
    Button b; 

    EditText et; 
    Button change; 
    TextView text; 

    String time; 

    int jClicked; 

    int i; 
    int j; 

    UserFunctions userFunction; 

    String [] aFromA; 
    String [] aToA; 
    String [] mobileA; 
    String [] LnameA; 
    String [] FnameA; 
    String [] fullName; 
    String [] aIDa; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.viewassistants); 

     text = (TextView) findViewById(R.id.textView1); 




     TableLayout tableLayout = (TableLayout) findViewById(R.id.myTableLayout); 

     userFunction = new UserFunctions(); 
     String DR_ID = userFunction.drID; 
     userFunction.viewingAssistants(DR_ID); 





     String ifFull =userFunction.full; 
     int ifFullint = Integer.parseInt(ifFull); 

     if (ifFullint == 1){ 

     text.setText(""); 
     TableRow tableRow; 
     TextView textView; 





     for (i = 0; i < (FnameA.length)+1; i++) { 
      tableRow = new TableRow(getApplicationContext()); 

      for (j = 0; j < 5; j++) { 

      if(i==0){ 
      textView = new TextView(getApplicationContext()); 
      textView.setBackgroundResource(R.drawable.nm); 
      textView.setTextColor(Color.BLACK); 
      tableRow.addView(textView); 

      switch (j){ 
      case 0:textView.setText(" ID ");break; 
      case 1:textView.setText(" Name ");break; 
      case 2:textView.setText(" Mobile "); break; 
      case 3:textView.setText(" Shift starts "); break; 
      case 4:textView.setText(" Ends "); break; 
      default: textView.setText(".."); 
      } 

      } 
      else 
      { 

      b = new Button(getApplicationContext()); 
      b.setBackgroundResource(R.drawable.nm); 
      b.setTextColor(Color.BLACK); 



      tableRow.addView(b); 



      for(int w=0;w<FnameA.length;w++){ 
       fullName[w]= FnameA[w]+" "+LnameA[w]; 
      } 

      switch (j){ 
      case 0:b.setText(""+aIDa[i-1]+"");break; 
      case 1:b.setText(""+fullName[i-1]+"");break; 
      case 2:b.setText(""+mobileA[i-1]+""); break; 
      case 3:b.setText(""+aFromA[i-1]+""); b.setOnClickListener(this);break; 
      case 4:b.setText(""+aToA[i-1]+"");b.setOnClickListener(this);break; 
      default: b.setText(".."); 
      } 

      } 
      } 
      tableLayout.addView(tableRow); 



      } 
     } 
     else{ 

      text.setText("NO PATIENTS"); 
      System.out.println("fel elseeee"); 
     } 


     back = (Button) findViewById(R.id.back); 
     back.setOnClickListener(new View.OnClickListener() { 


      @Override 
      public void onClick(View view) { 
       // TODO Auto-generated method stub 


       Intent dashboard = new Intent(getApplicationContext(), loginAsDr.class); 
       // Close all views before launching Dashboard 
       dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(dashboard); 
       // Close Registration Screen 
       finish(); 

      } 

     }); 



    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     et=(EditText)findViewById(R.id.shiftChange); 
     et.setVisibility(View.VISIBLE); 


     change=(Button) findViewById(R.id.change); 
     change.setVisibility(View.VISIBLE); 


     change.setOnClickListener(new View.OnClickListener() { 


      @Override 
      public void onClick(View view) { 
       // TODO Auto-generated method stub 



       jClicked=i; 
       time=et.getText().toString(); 

       et.setText(" "); 
       Log.d("TIME IS",time+" "+jClicked); 

      } 

     }); 
    } 
} 

和这里的XML:

如果
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
<HorizontalScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#3b3b3b" > 
<TableLayout 
    android:id="@+id/myTableLayout" 
    android:layout_width="match_parent" 
    android:layout_height="390dp" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="NO DOCTORS" /> 

</TableLayout> 
</HorizontalScrollView> 

<Button 
    android:id="@+id/back" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_marginBottom="40dp" 
    android:text="BACK" /> 


<EditText 
    android:id="@+id/shiftChange" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:visibility = "gone" 
    android:ems="10" /> 

<Button 
    android:id="@+id/change" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_toRightOf="@+id/shiftChange" 
    android:visibility = "gone" 
    android:text="Change please" /> 

</RelativeLayout> 

对不起码是一点点长。提前致谢。

+0

不能设置自己的ID .. ?或** setTag()**可以派上用场。 – ngesh 2013-03-21 10:37:52

+0

如何设置标签()以及它的具体操作是什么? – Salma 2013-03-21 10:39:05

+1

** setTag()**用于保留东西附加到视图..它可以是任何东西(我的意思是对象或整型)..稍后,当你需要它时,你说* * getTag()**与相应的视图对象..请参阅http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view – ngesh 2013-03-21 10:45:00

回答

1

使用setTag将是你最好的选择。您几乎可以将任何数据结构存储在其中。在这里,我推荐一个简单的Vector存储你的我的和j的,即:

Vector<Integer> v = new Vector<Integer>(); 
v.add(i); 
v.add(j); 
b.setTag(v); 

然后在点击的方法:在运行时

public void onClick(View v) { 
    Vector<Integer> v = (Vector<Integer>)v.getTag(); 
    int i = v.get(0); 
    int j = v.get(1); 
} 
+0

非常感谢!有效! – Salma 2013-03-21 11:01:26