2013-10-28 34 views
2

我发现了一段用于制作吐司消息的代码。作为一名新的android开发者,我知道,我们有一个监听器来使按钮工作。但这里没有听众。那么为什么这个代码工作?这段代码为什么会烤面包?没有听众

public class MainActivity extends Activity { 
private String mButtonMessageTemplate; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mButtonMessageTemplate=getString(R.string.button_messege_template); 
} 
public void showButtonText(View clickedButton){ 
Button button=(Button) clickedButton; 
CharSequence text=button.getText(); 
String message=String.format(mButtonMessageTemplate, text); 
showToast(message); 

}

public void showToast(String text) { 
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();  

}

另一个问题是

<Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hi_button_lebel" 
     android:onClick="showButtonText"/> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/bye_button_lebel" 
     android:onClick="showButtonText" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/yo_button_lebel" 
     android:onClick="showButtonText" /> 

机器人:的onClick = “showButtonText” 这是使用非onClickListener方法!怎么样?? 请给我一个详细的答案。提前致谢。 :)

+0

http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene – Delyan

回答

1
android:onClick="showButtonText" 

这就是你的听众。当单击该视图时,将执行showButtonText方法。

这是xml代码中的View.OnClickListener。当它被编译时,它会变成你非常喜欢的java代码。

注意:这是在xml代码中放置onclickListener的坏习惯。你应该做的是:

button1=(Button)findViewById(R.id.button1); 
button1.setOnClickListener(new View.OnClickListener(){ 
    public void onclick(View v){ 
    //do what happens on click of button1 
     showToast("button1 clicked."); 

    } 
}); 
+0

这可能有助于解释为什么* *你认为这是一个“坏习惯”。 – Geobits

+0

虐待后添加,但一个非常简单的。它很难找到!你的onclick逻辑有两个不同的文件!很难阅读。很难让初学者知道。此外,我不认为theres任何advangate把它放在XML中。 – wtsang02

0

的Android可以让你使用属性atoumatically创建调用您的Activity类方法的侦听器。

该方法必须接收类型为View的参数才能使其工作。

0

你所有的按钮都onclick属性设置为来电的方法

showButtonText(View clickedButton) 

这种方法有它的一份声明显示敬酒。

showToast(message); 

所以每次点击任何按钮时,它都会显示烤面包。

0

以下是View.java中的代码。
如果视图具有“onClick”属性,View的构造函数将创建并注册“OnClickListener”。

public View(Context context, AttributeSet attrs, int defStyle) { 
.... 
    case R.styleable.View_onClick: 
    if (context.isRestricted()) { 
     throw new IllegalStateException("The android:onClick attribute cannot " 
       + "be used within a restricted context"); 
    } 

    final String handlerName = a.getString(attr); 
    if (handlerName != null) { 
     setOnClickListener(new OnClickListener() { 
      private Method mHandler; 

      public void onClick(View v) { 
       if (mHandler == null) { 
        try { 
         mHandler = getContext().getClass().getMethod(handlerName, 
           View.class); 
        } catch (NoSuchMethodException e) { 
         int id = getId(); 
         String idText = id == NO_ID ? "" : " with id '" 
           + getContext().getResources().getResourceEntryName(
            id) + "'"; 
         throw new IllegalStateException("Could not find a method " + 
           handlerName + "(View) in the activity " 
           + getContext().getClass() + " for onClick handler" 
           + " on view " + View.this.getClass() + idText, e); 
        } 
       } 

       try { 
        mHandler.invoke(getContext(), View.this); 
       } catch (IllegalAccessException e) { 
        throw new IllegalStateException("Could not execute non " 
          + "public method of the activity", e); 
       } catch (InvocationTargetException e) { 
        throw new IllegalStateException("Could not execute " 
          + "method of the activity", e); 
       }  
      }    
     });    
    } 
    break; 
.... 
}