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

标签 java android xml button

在接下来的 Activity 中,我创建了一个表格并向其中动态添加按钮。按钮未在 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);
}

关于java - Android:确定单击了哪个按钮,因为该按钮未在 xml 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15545086/

相关文章:

Java 不打印整数变量

android - 如何使线性布局可滚动?

Java XML - 当我生成 XML 文件时,Java 将 me ' and "更改为 ' and "

java - 我的复制构造函数出了什么问题?

Java 当前时间比我的系统时间早 2 小时

android - 如何解决找不到com.google.gms :google-services:4. 1.0.?

Android WiFi 无法从 ScanResult 获取 SSID 和 BSSID

c# - Xml 反序列化为 System.Type

java - 比较 2D 数组 Java

android - android中应用程序的自动更新数据库