2012-06-08 10 views
5

Tôi biết điều này thật dễ dàng nhưng tôi không thể gỡ lỗi nó.Khi người nghe nhấp chuột được gọi hai lần

Tôi đang tạo động 3 LinearLayouts và thêm chúng vào chế độ xem cuộn. Tôi đang thêm một nút "ins" vào đầu 3 linearlayout.

Vấn đề là khi nhấp vào nút ins, nút trên danh sách nhấp chuột được gọi là 3 lần. mã:

package com.integrated.mpr; 

import android.app.Activity; 
import android.app.Dialog; 
import android.util.Log; 
import android.util.TypedValue; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.ScrollView; 
import android.widget.TextView; 


public class Page1 extends Activity implements OnClickListener{ 

    static int pos = new Choose().n; 
    static String partname; 

    int i; 
    int[][] id = new int[pos][5]; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     int p =0; 
     for(int i =0;i<3;i++){ 
      for(int j =0;j<5;j++){ 

       p++; 
      } 
     } 


     ScrollView sv = new ScrollView(this); 

     LinearLayout ll = new LinearLayout(this); 
     ll.setOrientation(LinearLayout.VERTICAL); 
     int resid = getResources().getIdentifier("background", "drawable", getPackageName()); 
     ll.setBackgroundResource(resid); 

     Button ins = new Button(this); 
     ins.setText("Instructions"); 
     ins.setOnClickListener(this); 
     ins.setId(5000); 

     ll.addView(ins); 

     for(i =0;i<3;i++){ 


      LinearLayout llay = new LinearLayout(this); 
      llay.setOrientation(LinearLayout.VERTICAL); 

      TextView tv = new TextView(this); 
      tv.setText("enter the " +(i+1)+" position name"); 


      EditText et = new EditText(this); 
      et.setId(id[i][0]); 

      Button starta = new Button(this); 
      starta.setText("Record 1"); 
      starta.setId(id[i][1]); 
      starta.setOnClickListener(this); 

      Button startb = new Button(this); 
      startb.setText("Record 2"); 
      startb.setId(id[i][2]); 
      startb.setOnClickListener(this); 


      Button startc = new Button(this); 
      startc.setText("Record 3"); 
      startc.setId(id[i][3]); 
      startc.setOnClickListener(this); 


      Button stop = new Button(this); 
      stop.setText("Submit"); 
      stop.setId(id[i][4]); 
      stop.setOnClickListener(this); 

      TextView tv1 = new TextView(this); 
      tv1.setVisibility(llay.INVISIBLE); 

      llay.addView(tv); 
      llay.addView(et); 
      llay.addView(starta); 
      llay.addView(startb); 
      llay.addView(startc); 
      llay.addView(stop); 
      llay.addView(tv1); 
      ll.addView(llay); 

     } 
     Button bcon = new Button(this); 
     bcon.setText("Continue"); 
     bcon.setId(10000); 
     bcon.setOnClickListener(this); 
     ll.addView(bcon); 


     sv.addView(ll); 

     this.setContentView(sv); 

    } 

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

      if(v.getId()==5000){  
      Log.d("ins", "called"); 
      Context mContext = Page1.this; 
      Dialog dialog = new Dialog(mContext); 
      dialog.setTitle("Instructions"); 
      dialog.setContentView(R.layout.instructiondialog); 
      dialog.show(); 


      } 

     } 
    } 






} 

Tôi biết chắc chắn, có một số vấn đề trong mã của OnCreate nhưng nó là gì? Trợ giúp xin vui lòng

Có ai tiếp tục giải thích cách đặt nút căn chỉnh ở bên phải màn hình không, đó là cách đặt trọng số bố cục của nó?

+1

Tôi đoán nó có cái gì để làm với tuyên bố nó bên trong vòng lặp for ... – GAMA

+0

Chính xác, nhưng bạn có thể chính xác hơn một chút –

+0

Như GAMA nói, nếu bạn lặp ba lần và xác định trình nghe onclick trong mỗi vòng lặp, bạn sẽ nhận được ba onclicklisteners, tất cả sẽ được gọi khi người dùng nhấn nút. – Christine

Trả lời

1

Tôi không chắc chắn về chính xác những gì bạn muốn đạt được. Nhưng bạn có thể đặt cờ.if giá trị cờ là 1, sau đó chỉ xử lý onClick khác không.

Đoạn mã dưới đây sẽ giúp bạn.

Có, nhưng bạn sẽ phải bằng cách nào đó thiết lập lại lá cờ đó để 1.

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

       if(v.getId()==5000) 
       {  
       if(flag) 
       { 
        Log.d("ins", "called"); 
        Context mContext = Page1.this; 
        Dialog dialog = new Dialog(mContext); 
        dialog.setTitle("Instructions"); 
        dialog.setContentView(R.layout.instructiondialog); 
        dialog.show(); 
       } 
       flag=0; 
       } 
      } 
1

Bạn cũng có thể gọi nút bấm sự kiện như thế này

Button ins = new Button(this); 
ins.setText("Instructions"); 
ins.setOnClickListener(this); 
ins.setId(5000); 

ins.setOnClickListener(new OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     // TODO Auto-generated method stub 
     Context mContext = Page1.this; 
     Dialog dialog = new Dialog(mContext); 
     dialog.setTitle("Instructions"); 
     dialog.setContentView(R.layout.instructiondialog); 
     dialog.show(); 
    } 
}); 
+0

Cảm ơn u .. công trình này –