使用按钮在Android中打开另一个活动

时间:2013-02-11 18:21:23

标签: android android-intent android-activity

我一定是做错了。我在第一个活动中有这个代码:

package com.Trenton.waziapp;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class WaziLoginScreen extends Activity implements OnClickListener{

EditText etUsername, etPassword;
Button bLogin;
Class ourClass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wazi_login_screen);

    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);
    bLogin = (Button) findViewById(R.id.bLogin);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.wazi_login_screen, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.bLogin:

        Intent a = new Intent(WaziLoginScreen.this, ProfileScreen.class);
        startActivity(a);
        break;

    }
}

} 我在上面做了什么错误,让其他Activity,ProfileScreen打开?

我的清单文件包含这样的活动条目 -

    <activity
        android:name=".ProfileScreen"
        android:label="@string/title_activity_profile_screen">
    </activity>

3 个答案:

答案 0 :(得分:4)

您忘记设置onclickListener了... 试试这个

yourButtonObject.setOnClickListener(this);

答案 1 :(得分:3)

在WaziLoginScreen类中的OnCreate方法中编写以下代码 -

yourButtonObject.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent a = new Intent(WaziLoginScreen.this, ProfileScreen.class);
        startActivity(a);
    }
});

就是这样。

答案 2 :(得分:1)

使用以下代码。

package com.Trenton.waziapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class WaziLoginScreen extends Activity {

EditText etUsername, etPassword;
Button bLogin;
Class ourClass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wazi_login_screen);

    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);
    bLogin = (Button) findViewById(R.id.bLogin);

    bLogin.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent a = new Intent(WaziLoginScreen.this, ProfileScreen.class);
        startActivity(a);
    }
 }); 
 }

          @Override
           public boolean onCreateOptionsMenu(Menu menu) {
                  // Inflate the menu; this adds items to the action bar if it is present.
                  getMenuInflater().inflate(R.menu.wazi_login_screen, menu);
                 return true;
          }

}

多数民众赞成...很高兴。

相关问题