簡體   English   中英

Android:確定在xml中未定義該按鈕時單擊了哪個按鈕

[英]Android: Determine which button was clicked given that button not defined in xml

在下面的活動中,我創建了一個表並動態添加按鈕。 按鈕沒有在xml中定義,我想知道在哪個位置點擊了按鈕(標記為'b')。 即:其中i和j我嘗試將jClickable設置為i,但它將其設置為最終值,因為它是在創建表后單擊的。

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>

對不起,如果代碼有點長。 提前致謝。

使用setTag將是您最好的選擇。 您可以在其中存儲幾乎任何數據結構。 在這里,我推薦一個簡單的Vector來存儲你的i和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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM