[英]I set up a switch-statement to handle OnClick events in my Activity, but only the first case works. Why?
我在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;
}
}
}
在我的主要活动中,我在开关语句中创建了案例来处理OnClick事件,但是只有第一个案例定义有效。 其余的情况永远不会因某种原因触发或长时间延迟后触发。 为了诊断问题出在哪里,我有:
1.试图更改添加侦听器的顺序
2.更改了大小写,第一个是CloseActivityCross,然后是SignInButton。 每次只有第一种情况能正常工作。
为什么会这样? 我应该提到,在一次试验中,事情在一段时间内开始运转良好,现在出现了同样的问题。
不要在onClick方法中重新初始化视图,即,从OnClick
方法中删除这些行,这应该很好。
EditText email = (EditText) findViewById(R.id.EmailEditText);
EditText password = (EditText) findViewById(R.id.PasswordEditText);
TextView close = (TextView) findViewById(CloseActivityCross);
Button sign_up = (Button) findViewById(R.id.SignUpButton);
Button sign_in = (Button) findViewById(R.id.SignInButton);
发生的事情是,当您按任何这些视图时,它们将在您再次初始化以上视图时丢失其onClickListeners
。
编辑为OP添加了其他代码。
编辑2:
删除根元素RelativeLayout的OnClick
<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"
tools:context="com.radical.pillbox.MainActivity">
在Java代码中, Listeners
是可以的。 问题出在xml中。 您的父布局是RelativeLayout
并且您使用了规则android:layout_below
。
在某些地方,您使用"@id
代替"@+id
。 因此,这将使视图重叠。 使它在所有android:layout_below
标记中均写为"@+id
,并从根布局中删除android:onClick="onClick"
,看看是否android:layout_below
。
建议 :-如果所有小部件都垂直对齐,请使用LinearLayout
垂直对齐。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.