I'm developing an android app. There will a view like timetable on screen.I wanna create this view with using multiple buttons. For example like this:
|08.30| |09.30| |10.30|
|11.30| |12.30| |13.30|
|14.30| |15.30| |16.30|
User will click one of buttons, and I will get button's text. I need to same event to buttons for get them text. But, there will be buttons more than 20 and I guess that process isn't useless for multiple buttons. What is the best and easiest way for set same event to buttons? Am I need to describe all buttons in java side?
Method 1:
Create a function and pass text of buttons as variable to it:
onclick()
{
func(Button.getText().toString());
}
func(String s)
{
//do something
}
you will have to override onClick of each button in the above method.
Method 2:
in xml define
<Button>
...
android:onClick ="func"
..
</Button>
and in Java:
func()
{
//Do something.
}
You wont get the text in above method.
Method 3:
Implement the onClickListener:
public class MainActivity extends Activity implements OnClickListener {
....
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
func(button1.getText().toString());
break;
case R.id.button2:
func(..)
break;
....
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.