有 Java 编程相关的问题?

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

java Android:确定单击了哪个按钮,因为该按钮未在xml中定义

在下面的活动中,我创建了一个表并向其中动态添加按钮。这些按钮没有在xml中定义,我想知道在哪个位置单击了按钮(标记为“b”)。i、 e:其中我和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:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent" >
<HorizontalScrollView
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent"
    安卓:background="#3b3b3b" >
<TableLayout
    安卓:id="@+id/myTableLayout"
    安卓:layout_width="match_parent"
    安卓:layout_height="390dp" >

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

</TableLayout>
</HorizontalScrollView>  

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


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

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

</RelativeLayout>

抱歉,如果代码有点长。提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    使用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);
    }