android按钮onClick-in xml引用参数

android按钮onClick-in xml引用参数,android,Android,只是为了学习 <Button android:id="@+id/button" android:text="" android:layout_width="fill_parent" android:layout_height="fill_parent" android:onClick="updateTime"/> public class MainActivity extends Activity { Button btn; @Override public void onC

只是为了学习

<Button 
android:id="@+id/button"
android:text=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="updateTime"/>


public class MainActivity extends Activity {
Button btn;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    btn = (Button) findViewById(R.id.button);
    updateTime(btn);

}

public void updateTime(View b) {
    btn.setText(new Date().toString());
}
但它不起作用 你能帮帮我吗

提前感谢。

您是否尝试过:

public void updateTime(View b) {
    ((Button)b).setText(new Date().toString());
}
从:

此名称必须对应于只接受一个名称的公共方法 类型视图的参数

因此,如果将updateTime定义为xml布局中的单击操作侦听器,则回调必须与签名
public void updateTime(视图v)
匹配,这意味着您不能将其声明为接受按钮作为参数

另外,
android:onClick
仅适用于API级别4及以上,因此如果您的目标API级别低于该级别,则必须在代码中声明单击操作侦听器

public void updateTime(View b) {
    ((Button)b).setText(new Date().toString());
}