开发者

how to call which button to work in android

开发者 https://www.devze.com 2023-03-13 07:02 出处:网络
I have an activity class with many buttons. If i click one button then it will go to next page then go back to main class.If I click another button in main class, it will go to next page together with

I have an activity class with many buttons. If i click one button then it will go to next page then go back to main class.If I click another button in main class, it will go to next page together with data. Do anyone know how to write the function in activity class?

Can I write like this in a class? But when i run it only one button is working , when i clicked other i get error. I am new to android ,so please give me suggestion.

public class MyClass extends Activity {

private Button button,button1,button2; 

public void onCreate(){.... initControl()开发者_如何转开发;}

 public void initControl() { button=(Button)findViewById(R.id.button); .....
 button.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
 button1.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
 button2.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})

 }

thanks for help.


First initialize button1, button2 before overriding onCreate(). Then assign the values in oncreateMethod call initializemthod

Have a look on the following code

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class WebViewTest extends Activity {
    Button button1 = null;
    Button button2 = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button1 = (Button) findViewById(R.id.firstbutton);
        button2 = (Button) findViewById(R.id.secondbutton);
        initControl();
    }
public void initControl() { 
button1.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
});
button2.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
});


}
}

Thanks Deepak


You have to add on click method

0

精彩评论

暂无评论...
验证码 换一张
取 消