我在Android项目中创建了一个空活动,并添加了一个TextView和一个Button,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:onClick="onClick"
tools:context="com.radical.pillbox.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/CloseActivityCross"
android:text="@string/cross"
android:textSize="50sp"
android:padding="10dp"
android:fontFamily="@font/montserrat_bold"
android:textColor="@color/pillbox_green_primary_dark" />
<TextView
android:id="@+id/IntroString"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/CloseActivityCross"
android:fontFamily="@font/montserrat_light"
android:padding="10dp"
android:text="Welcome to Pillbox."
android:textColor="#000000"
android:textSize="30sp"
android:gravity="left"
android:textAlignment="gravity"/>
<EditText
android:id="@+id/EmailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/IntroString"
android:layout_marginLeft="10dp"
android:alpha="0.64"
android:fontFamily="@font/montserrat_light"
android:hint="email@example.com"
android:inputType="textEmailAddress"
android:textColor="@color/pillbox_background_dark"
android:textColorHint="@color/pillbox_accent_green" />
<EditText
android:id="@+id/PasswordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/EmailEditText"
android:layout_marginStart="10dp"
android:alpha="0.64"
android:fontFamily="@font/montserrat_light"
android:hint="password"
android:inputType="textPassword"
android:textColor="@color/pillbox_background_dark"
android:textColorHint="@color/pillbox_accent_green" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/FooterString"
android:textColor="#000000"
android:textSize="12sp"
android:text="2018."
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:fontFamily="@font/montserrat_light"
android:layout_alignParentBottom="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:id="@+id/SignInButton"
android:background="@drawable/round_button"
android:text="Sign In"
android:textSize="15sp"
android:textColor="@color/pillbox_green_primary_dark"
android:layout_below="@id/PasswordEditText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:id="@+id/SignUpButton"
android:background="@drawable/round_button"
android:text="Sign Up"
android:textSize="15sp"
android:textColor="@color/pillbox_green_primary_dark"
android:layout_below="@id/PasswordEditText"
android:layout_toRightOf="@id/SignInButton"/>
</RelativeLayout>
我的 MainActivity.java 文件是:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText email = (EditText) findViewById(R.id.EmailEditText);
EditText password = (EditText) findViewById(R.id.PasswordEditText);
TextView close = (TextView) findViewById(R.id.CloseActivityCross);
Button sign_up = (Button) findViewById(R.id.SignUpButton);
Button sign_in = (Button) findViewById(R.id.SignInButton);
email.setOnClickListener(MainActivity.this);
password.setOnClickListener(MainActivity.this);
sign_in.setOnClickListener(MainActivity.this);
close.setOnClickListener(MainActivity.this);
}
@Override
public void onClick(View view) {
EditText email = (EditText) findViewById(R.id.EmailEditText);
EditText password = (EditText) findViewById(R.id.PasswordEditText);
TextView close = (TextView) findViewById(R.id.CloseActivityCross);
Button sign_up = (Button) findViewById(R.id.SignUpButton);
Button sign_in = (Button) findViewById(R.id.SignInButton);
switch (view.getId()) {
case (R.id.SignInButton):
Log.d("Event:", "Signed in with correct credentials.");
break;
case (R.id.CloseActivityCross):
Toast.makeText(MainActivity.this, "Closing.", Toast.LENGTH_SHORT).show();
MainActivity.this.finish();
Log.d("Cross:", "Activity closed");
break;
}
}
}
在我的主要活动中,我在switch语句中创建了用于处理OnClick事件的案例,但只有第一个案例定义了。其余的情况永远不会因某种原因触发或在延迟延迟后触发。为了诊断出现问题的地方,我有:
1.试图改变我添加听众的顺序
2.改变案例,第一个是CloseActivityCross一次,SignInButton是下一个。每一次,只有第一种情况才能正常工作。
为什么会这样?我应该提一下,在一次试验中,事情开始有效地进行了一段时间,现在同样的问题正在出现。