jjzjj

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

coder 2024-07-02 原文

在接下来的 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:确定单击了哪个按钮,因为该按钮未在 xml 中定义的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  4. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  5. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  6. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  7. ruby - 即时确定方法的可见性 - 2

    我正在编写一个方法,它将在一个类中定义一个实例方法;类似于attr_accessor:classFoocustom_method(:foo)end我通过将custom_method函数添加到Module模块并使用define_method定义方法来实现它,效果很好。但我无法弄清楚如何考虑类(class)的可见性属性。例如,在下面的类中classFoocustom_method(:foo)privatecustom_method(:bar)end第一个生成的方法(foo)必须是公共(public)的,第二个(bar)必须是私有(private)的。我怎么做?或者,如何找到调用我的cust

  8. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  9. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  10. ruby - 如何在 Grape 中定义哈希数组? - 2

    我使用Ember作为我的前端和GrapeAPI来为我的API提供服务。前端发送类似:{"service"=>{"name"=>"Name","duration"=>"30","user"=>nil,"organization"=>"org","category"=>nil,"description"=>"description","disabled"=>true,"color"=>nil,"availabilities"=>[{"day"=>"Saturday","enabled"=>false,"timeSlots"=>[{"startAt"=>"09:00AM","endAt"=>

随机推荐