[英]ButtonOnClick method doesn't invoke on first click
Everything was fine until I decided to put the buttons in a fragment. 一切都很好,直到我决定把按钮放成一个片段。 After I changed my code to what u see below app started to ask for a 2nd click to invoke button on clicks when it first starts.
在将代码更改为您在下面看到的内容后,应用程序开始要求第二次点击以在首次启动时点击按钮。 For example, when I click 1st button nothing happens but if I click it a 2nd time it invokes the onclick and keeps working as long as I don't restart the app;
例如,当我单击第一个按钮时,什么也没有发生,但是如果我第二次单击它,它将调用onclick并保持工作,只要不重启应用程序即可; so I can click all the other buttons and when I click the first one again it still works.
因此,我可以单击所有其他按钮,当我再次单击第一个按钮时,它仍然有效。 I would appreciate if u could point out the problem.
如果您能指出问题,我将不胜感激。
Regards, Wince 问候,温斯
MainActivity; 主要活动;
package wince.deneme;
import android.content.res.Configuration;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buttonfragment);
final Button Button1 = (Button) findViewById(R.id.button1);
final Button Button2 = (Button) findViewById(R.id.button2);
final Button Button3 = (Button) findViewById(R.id.button3);
final Button Button4 = (Button) findViewById(R.id.button4);
final Button Button5 = (Button) findViewById(R.id.button5);
final Button Button6 = (Button) findViewById(R.id.button6);
final Button Button7 = (Button) findViewById(R.id.button7);
final Button Button8 = (Button) findViewById(R.id.button8);
final Button Button9 = (Button) findViewById(R.id.button9);
final Button Button10 = (Button) findViewById(R.id.button10);
final Button Button11 = (Button) findViewById(R.id.button11);
final Button Button12 = (Button) findViewById(R.id.button12);
final Button Button13 = (Button) findViewById(R.id.button13);
Button1.setOnClickListener(this);
Button2.setOnClickListener(this);
Button3.setOnClickListener(this);
Button4.setOnClickListener(this);
Button5.setOnClickListener(this);
Button6.setOnClickListener(this);
Button7.setOnClickListener(this);
Button8.setOnClickListener(this);
Button9.setOnClickListener(this);
Button10.setOnClickListener(this);
Button11.setOnClickListener(this);
Button12.setOnClickListener(this);
Button13.setOnClickListener(this);
}
@Override
public void onClick(View v) {
{
Button Button1 = (Button) v;
if(Button1.getText()=="Button")
switch (v.getId()) {
case R.id.button1:
Button1.setText("1");
break;
case R.id.button2:
Button1.setText("2");
break;
case R.id.button3:
Button1.setText("3");
break;
case R.id.button4:
Button1.setText("4");
break;
case R.id.button5:
Button1.setText("5");
break;
case R.id.button6:
Button1.setText("6");
break;
case R.id.button7:
Button1.setText("7");
break;
case R.id.button8:
Button1.setText("8");
break;
case R.id.button9:
Button1.setText("9");
break;
case R.id.button10:
Button1.setText("10");
break;
case R.id.button11:
Button1.setText("11");
break;
case R.id.button12:
Button1.setText("12");
break;
case R.id.button13:
Button1.setText("13");
break;
default:
Button1.setText("Hatalı");
break;
}
else
Button1.setText("Button");
}
}
}
FragmentActivity 片段活动
package wince.deneme;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class ButtonFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.buttonfragment,container,false);
}
}
Fragment.xml; Fragment.xml;
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="0dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toBottomOf="@id/button1"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toBottomOf="@id/button2"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toBottomOf="@id/button3"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toBottomOf="@id/button4"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toBottomOf="@id/button5"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toBottomOf="@id/button6"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toBottomOf="@id/button7"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toBottomOf="@id/button8"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toBottomOf="@id/button9"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button11"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toBottomOf="@id/button10"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button12"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toBottomOf="@id/button11"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button13"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toBottomOf="@id/button12"
app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
Edit did a little debuging with putting a logcat at here; Edit在此处放置了一个logcat进行了一些调试。
Button Button1 = (Button) v;
Log.d("Db", "onClick: "+Button1.getText());
if (Button1.getText().toString() == "Button")
result is ; 结果是;
10-18 11:38:25.422 23055-23055/wince.deneme D/Db: onClick: Button // clicked 1st Button
10-18 11:38:27.154 23055-23055/wince.deneme D/Db: onClick: Button // clicked 1st Button
10-18 11:38:28.838 23055-23055/wince.deneme D/Db: onClick: 1 // clicked 1st Button
10-18 11:38:29.702 23055-23055/wince.deneme D/Db: onClick: Button // clicked 1st Button
10-18 11:38:31.318 23055-23055/wince.deneme D/Db: onClick: 1 // clicked 1st Button
10-18 11:38:33.862 23055-23055/wince.deneme D/Db: onClick: Button // clicked 1st Button
10-18 11:38:38.954 23055-23055/wince.deneme D/Db: onClick: Button // clicked 2nd Button
10-18 11:38:40.678 23055-23055/wince.deneme D/Db: onClick: Button // clicked 2nd Button
Try to set android:focusable= true and android:focusableInTouchMode=true to ScrollView. 尝试将android:focusable = true和android:focusableInTouchMode = true设置为ScrollView。 it will work
它会工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.