在Android的按钮工作 [英] Working with buttons in android

查看:140
本文介绍了在Android的按钮工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我一直在做的应用程序,我想创建很大的进步,但大部分我一直只从学习教程展示其应用在内部只有一个活动部件的奇妙功能一时间...

问题是,我的应用程序需要2个或更多的按钮和这就是我的部分卡在一部分。我的code实现如下所示(一切声明)

一个SetWordsBtn

  

公共无效的onCreate(捆绑冰柱){

  super.onCreate(冰柱);
   的setContentView(R.layout.main);   SetWordsBtn =(按钮)findViewById(R.id.SetWordsBtn);   SetWordsBtn.setOnClickListener(本);


  
  

}


它实现了一个的onClick()是这样的:


  

公共无效的onClick(查看视图){

  startWords();


  
  

}


但如果我还有一个按钮删除诸如DelWordsBtn?我想我可以同时申报两个按钮是这样的:


  SetWordsBtn =(按钮)findViewById(R.id.SetWordsBtn);
  DelWordsBtn =(按钮)findViewById(R.id.DelWordsBtn);  SetWordsBtn.setOnClickListener(本);
  DelWordsBtn.setOnClickListener(本);


但什么的onClick()方法?它本身会自动应用到这两个按钮,当我做到这一点?

我怎么能够从对方声明一个单独的onClick所以它既没有不同的东西,当我点击其中任何一个?

我想答案可能是这样,但我不知道:


  //声明
  SetWordsBtn =(按钮)findViewById(R.id.SetWordsBtn);
  DelWordsBtn =(按钮)findViewById(R.id.DelWordsBtn);  SetWordsBtn.setOnClickListener(setWordsView);
  DelWordsBtn.setOnClickListener(delWordsView);  //功能的onClick


  
  

公共无效的onClick(查看setWordsView){

  startWords();


  
  

}


  
  

公共无效的onClick(查看delWordsView){

  deleteWords();


  
  

}


因此​​,这将实际上链接startWords()函数的SetWordsBtn,和deleteWords()以DelWordsBtn ...

帮助任何明确的解释/形式是AP preciated。在此先感谢球员。 :)


解决方案

典型的惯例是只关掉被点击查看的身份证。例如:

  View.OnClickListener监听器=新View.OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        开关(v.getId()){
            案例R.id.SetWordsBtn:
                startWords();
                打破;
            案例R.id.DelWordsBtn:
                deleteWords();
                打破;
        }
    }
};INT [] = IDS {R.id.SetWordsBtn,R.id.DelWordsBtn};对于(INT I:IDS)((按钮)findViewById(I))setOnClickListener(听众)。

Alright, so i've been making great progress on the app i'm trying to create, but most of the tutorials that i've been learning from only showcase the wondrous feature of having only one active widget inside the application at a time...

The thing is, my application requires 2 or more buttons and that's the part i'm partially stuck at. My code implements a "SetWordsBtn" shown below (everything else is declared),

public void onCreate(Bundle icicle) {

   super.onCreate(icicle);
   setContentView(R.layout.main);

   SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn);

   SetWordsBtn.setOnClickListener(this);

}

which implements a onClick() like this:

public void onClick(View view) {

  startWords();

}

but what if i have another button that deletes the words such as "DelWordsBtn"? I was thinking i could declare both buttons simultaneously like this:

  SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn);
  DelWordsBtn=(Button)findViewById(R.id.DelWordsBtn);

  SetWordsBtn.setOnClickListener(this);
  DelWordsBtn.setOnClickListener(this);

but what about the onClick() method? Does it automatically apply itself to both the buttons when i do this?

How am i able to declare a seperate onClick from each other so it both does different stuff when i click on either one of them?

I was thinking the answer could be something like this, but i dunno :

  //Declarations
  SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn);
  DelWordsBtn=(Button)findViewById(R.id.DelWordsBtn);

  SetWordsBtn.setOnClickListener(setWordsView);
  DelWordsBtn.setOnClickListener(delWordsView);

  //onClick Functions

public void onClick(View setWordsView) {

  startWords();

}

public void onClick(View delWordsView) {

  deleteWords();

}

So it would actually link the startWords() function to the SetWordsBtn, and deleteWords() to DelWordsBtn...

Any clear cut explanation/form of help would be appreciated. Thanks in advance guys. :)

解决方案

The typical convention is to just switch off of the ID of the View that is clicked. For example:

View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.SetWordsBtn:
                startWords();
                break;
            case R.id.DelWordsBtn:
                deleteWords();
                break;
        }
    }
};

int[] ids = { R.id.SetWordsBtn, R.id.DelWordsBtn };

for(int i : ids) ((Button)findViewById(i)).setOnClickListener(listener);

这篇关于在Android的按钮工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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