在点击调用两次监听器 [英] On click Listener called twice

查看:183
本文介绍了在点击调用两次监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是容易的,但读音字只是无法调试。

I know this is easy but i m just not able to debug it.

我是动态创建3 LinearLayouts并将其添加到滚动视图。我加入一个按钮项3的LinearLayout的顶部。

I am dynamically creating 3 LinearLayouts and adding them to a scroll View . I am adding a Button "ins" to the top of 3 linearlayout.

的问题是,上点击项按钮,上clicklistener其被称为3次。
code:

The problem is that on clicking the ins Button , its on clicklistener is being called 3 times . code:

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();


            }

        }
    }






}

我知道肯定,有一些问题,在OnCreate中的code,但它是什么?请帮助

I know definitely , there is some problem in the code of OnCreate but what it is ? Help please

任何人都可以进一步解释如何设置输出画面的右对齐按钮,那就是如何设置它的布局重心?

Can anyone further explain how to set the button aligned o the right of screen , that is how to set its layout gravity?

推荐答案

我不知道究竟要achieve.but可以设置flag.if标志值的是1,则只能处理其他的onClick没有。

I am not sure about what exactly you want to achieve.but you can set the flag.if flag value is 1 then only process onClick else not.

下面片段将帮助你。

是的,但你必须该标志在某种程度上重置为1。

Yes but you will have to somehow reset that flag to 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;
                }
             }

这篇关于在点击调用两次监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆