android eclipse button OnClick event

匿名 (未验证) 提交于 2019-12-03 02:01:02

问题:

I have 2 files: main_activity.xml and home.xml. I made a button in main_activity.xml

Here is the code snippet:

  

And then, I have my home.xml. I want the button to open up home.xml. How can i do this? I don't know any java and I am new to android development.

Here is my home.xml below:

And below is my AndroidManifest.xml:

And that's all I have. Please, if you reply, tell me where to add the code such as the directory or between code snippets.

回答1:

For managing click activity in android ,you can do as below

  1. Implement OnClickListener on YourActivity.java class like

    public class MainActivity extends Activity implements OnClickListener

  2. Then, declare your button in .java class like

    Button btn = (Button) findViewById(R.id.btnPlay);

  3. Then use button btn variable as below

    btn.setOnClickListener(new View.OnClickListener() {      public void onClick(View v) {         myClick(v); /* my method to call new intent or activity */     } }); 
  4. Handle the click event:

    public void myClick(View v) {     Intent intent = new Intent(**this, Swipe.class**);     startActivity(intent);// for calling the activity } 

you also need to register your activity(.java) in android manifest as below



回答2:

You can use this code.

Android: OnClickListener

In our activity class we add the onclick method.

In our activity class we add the onclick method.

    //On click event for button1 public void button1OnClick(View v) {     //Inform the user the button has been clicked     Toast.makeText(this, "Button1 clicked.", Toast.LENGTH_SHORT).show();                } 

In the layout file we add a reference to the onclick handler in the activity. The app will automatically bind the onclick method to the view (in this case button1)

 


回答3:

create another class goto your project right click and click class and create Home. In that Home class file extends activity and add code like this

public class Home extends Activity {   @Override   public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.home);   } } 

in splash activity class add this line

Intent intent = new Intent(SplashActivity.this,Home.class); startActivity(intent); 

add Home activity class in your android manifest file



回答4:

I will give you just a little bit to get you started as this answer may help others that are having trouble with using onClick() this way but you really need to learn Java and go through the Android Docs so you can ask better questions

You need to read Here about Actviities and how to create them. Then in your code you will have a function

 public void home(View v)  //the name of this function comes from where you declared in your manifest `android:onClick="home" {      Intent intent (MainActivity.this, HomeActivity.class); //MainActivity is the name of current activity and HomeActivity is the name of the activity you want to start      can add intent extras/flags/categories here      startActivity(intent); } 

You also need to add the HomeActivity in your manifest as you have for the other Activities.

But you really need to go through the docs and do some tutorials to get an idea of how the Android framework operates and you need to learn Java to make your life a whole lot easier. Besides the previous two links I have given, also see this post about click events as there are different ways to use onClick()

I hope this is enough to get you started and I really hope you go through the docs to get a better understanding of what you are doing. Good luck to you!

Another important link to get started

Intents



回答5:

android:onClick was added in API level 4 to make it easier, more Javascript-web-like, and drive everything from the XML. What it does internally is add an OnClickListener on the Button, which calls your home method.

 

.

public void home(View view){   Intent intent=new Intent (view.getContext(),Luton.class);   this.startActivity(intent); } 

In your activity class

Using java code you can do button click by getting the id of the button from xml.

 

.

Button button= (Button) findViewById(R.id.myHomeButton); button.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View v) {      //do whatever you want on button click   } });    

Both are exactly the same



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!