Android Button Onclick

后端 未结 9 1993
猫巷女王i
猫巷女王i 2020-11-29 03:41

OK I\'m new to android dev\'s and Java, so I\'m having problems with on click method here\'s my code. I know I\'ve gotta be close, thanks in advance. All I want my button to

相关标签:
9条回答
  • 2020-11-29 04:33

    Use something like this :

       public void onClick(View v) {
                // TODO Auto-generated method stub
               startActivity(new Intent("com.droidnova.android.splashscreen.MyApp"));
            }
    
    0 讨论(0)
  • 2020-11-29 04:35

    It would be helpful to know what code you are trying to execute when the button is pressed. You've got the onClick property set in your xml file to a method called setLogin. For clarity, I'd delete this line android:onClick="setLogin" and call the method directly from inside your onClick() method.

    Also you can't just set the display to a new XML, you need to start a new activity with an Intent, a method something like this would be appropriate

     private void setLogin() {
    
     Intent i = new Intent(currentActivity.this, newActivity.class);
     startActivty(i);
    
     }
    

    Then set the new Activity to have the new layout.

    0 讨论(0)
  • 2020-11-29 04:37

    this will sort it for you

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Button but1=(Button)findViewById(R.id.button1);
    
        but1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                Intent int1= new Intent(MainActivity.this,xxactivity.class);
                startActivity(int1);
            }
        });
    }
    

    You just need to amend the xxactivity to the name of your second activity

    0 讨论(0)
提交回复
热议问题