2017-01-16 128 views
0

當我點擊註冊它按預期工作。但是當我點擊忘記密碼它首先打開RegisterActivity,當我點擊物理後退按鈕時,它會打開ForgotActivity。OnClickListener - 多個TextView無法正常工作

我是在android編程新手。請幫忙。

的XML如下:...

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:padding="10dp" 
    tools:context="com.shwetait.mcafeerewards.LoginActivity" 
    android:layout_height="match_parent"> 

    <TextView 
     android:text="Email ID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <EditText 
     android:id="@+id/etemailid" 
     android:layout_marginBottom="10dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:text="Password" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <EditText 
     android:id="@+id/etpassword" 
     android:layout_marginBottom="10dp" 
     android:inputType="textPassword" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:id="@+id/bLogin" 
     android:text="Login" 
     android:layout_marginBottom="25dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:id="@+id/oPenRegister" 
     android:text="Register" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     android:clickable="true" 
     android:textStyle="bold" 
     android:layout_marginBottom="25dp" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:id="@+id/oPenForgot" 
     android:text="Forgot Password" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     android:clickable="true" 
     android:textStyle="bold" 
     android:layout_marginBottom="10dp" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

的的.java如下

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{ 

    Button bLogin; 
    EditText etemailid,etpassword; 
    TextView oPenRegister,oPenForgot; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     etemailid = (EditText) findViewById(R.id.etemailid); 
     etpassword = (EditText) findViewById(R.id.etpassword); 
     bLogin = (Button) findViewById(R.id.bLogin); 
     bLogin.setOnClickListener(this); 
     oPenRegister = (TextView) findViewById(R.id.oPenRegister); 
     oPenRegister.setOnClickListener(this); 
     oPenForgot = (TextView) findViewById(R.id.oPenForgot); 
     oPenForgot.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()){ 
      case R.id.bLogin: 
       break; 
      case R.id.oPenForgot: 
       startActivity(new Intent(this, ForgotActivity.class)); 
      case R.id.oPenRegister: 
       startActivity(new Intent(this, RegisterActivity.class)); 
     } 
    } 
} 

回答

2

你不必在你的情況下break;聲明。試試這個:

@Override 
public void onClick(View v) { 
    switch (v.getId()){ 
     case R.id.bLogin: 
     break; 
     case R.id.oPenForgot: 
      startActivity(new Intent(this, ForgotActivity.class)); 
      break; 

     case R.id.oPenRegister: 
      startActivity(new Intent(this, RegisterActivity.class)); 
      break; 

    } 
}