0
2つのボタン(1つの増分、1つの減分)でテキストビューのテキストを増減する必要があります。私は断片の中ですべてをやっています。そして、私はそれが少し違っていることに気付きました。ボタンをフラグメントで使用する
これらはボタンとTextViewのある
<Button
android:layout_width="36dp"
android:layout_height="match_parent"
android:layout_marginLeft="90dp"
android:text="-"
android:id="@+id/minus"
android:onClick="decrement"
/>
<TextView
android:layout_width="36dp"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:textSize="18sp"
android:id="@+id/text"
android:textAlignment="center"
android:texr="stats"/>
<Button
android:layout_width="36dp"
android:layout_height="match_parent"
android:text="+"
android:id="@+id/plus"
android:onClick="increment"
/>
これは
public class MyFragment extends Fragment {
int stat= 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_my_fragment, container, false);
}
public void increment(View view){
stat= stat+ 1;
displaytext();
}
public void decrement(View view){
stat = stat - 1;
displaytext();
}
public void displaytext(){
TextView textViewevstat = (TextView) getView().findViewById(R.id.text);
textViewevstat.setText("" + stat);
}
}
それは私に、構文エラーやを取ることはありませんフラグメントのコードで似ていますが、エミュレータ、私は押してアプリのクラッシュで1つのボタン。 私はこの道を他の時間にリメイクする必要がありますが、この場合、IDとメソッドの名前を変更するには十分だと思います。ここで