Button响应的两种方法
1.implement buttonlistener;
package carrot.android.pr.layout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Date;
public class newlayout extends Activity
implements View.OnClickListener {
private Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.Button01);
btn.setOnClickListener(this);
btn.setText(new Date().toString());
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn.setText(new Date().toString());
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Date;
public class newlayout extends Activity
implements View.OnClickListener {
private Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.Button01);
btn.setOnClickListener(this);
btn.setText(new Date().toString());
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn.setText(new Date().toString());
}
}
2.xml
<Button android:text="@+id/Button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@string/buttonhanlder"></Button>
<string name="buttonhanlder">myClick</string>
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@string/buttonhanlder"></Button>
<string name="buttonhanlder">myClick</string>
public class newlayout extends Activity {
private Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.Button01);
btn.setText(new Date().toString());
}
public void myClick(View v) {
btn.setText(new Date().toString());
}
}
private Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.Button01);
btn.setText(new Date().toString());
}
public void myClick(View v) {
btn.setText(new Date().toString());
}
}