我是Android Studio的新手,我有几个问题。为了开始熟悉,我创建了一个简单的计算器程序,其中同时包含复选框和单选按钮。尽管我进一步进行了一项主要活动,但我对它感到满意的是复选框计算器活动和无线电计算器活动。但是,我想进一步介绍一下,并尝试对两个计算器使用导航抽屉。经过一些调整和测试之后,我设法了解了什么使之起作用。后来我遇到了一些片段,这些片段与活动有所不同。我读到,在诸如收音机之类的情况下,必须以编程方式实现onclick。我想知道如何将以下代码更改为片段可以处理的内容。
public class radio extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio);
}
public void onRadioButtonClicked(final View view) {
final boolean checked = ((RadioButton) view).isChecked();
Button button = (Button) findViewById(R.id.calBtn);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText num1editTxt = (EditText) findViewById(R.id.num1editTxt);
EditText num2editTxt = (EditText) findViewById(R.id.num2editTxt);
TextView resultViewTxt = (TextView) findViewById(R.id.resultViewTxt);
if (TextUtils.isEmpty(num1editTxt.getText().toString()) || TextUtils.isEmpty(num2editTxt.getText().toString())){
Toast.makeText(radio.this, "Empty field not allowed!", Toast.LENGTH_LONG).show();
}
else {
int num1 = Integer.parseInt(num1editTxt.getText().toString());
int num2 = Integer.parseInt(num2editTxt.getText().toString());
int result = 0;
switch (view.getId()) {
case R.id.addRadio:
if (checked) {
result = num1 + num2;
resultViewTxt.setText(result + "");
}
break;
case R.id.subRadio:
if (checked) {
result = num1 - num2;
resultViewTxt.setText(result + "");
}
break;
case R.id.multiRadio:
if (checked) {
result = num1 * num2;
resultViewTxt.setText(result + "");
}
break;
case R.id.divRadio:
if (checked) {
result = num1 / num2;
resultViewTxt.setText(result + "");
}
break;
}
}
}
});
}
}
这是从活动中复制的ui片段的xml,没有任何更改:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/num1editTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="104dp"
android:ems="10"
android:hint="@string/hint"
android:importantForAutofill="no"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/num2editTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:ems="10"
android:hint="@string/hint"
android:importantForAutofill="no"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/num1editTxt" />
<RadioGroup
android:id="@+id/radiGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/calBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/num2editTxt">
<RadioButton
android:id="@+id/addRadio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{() -> fragment.buttonClicked()}"
android:text="@string/add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="@+id/subRadio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/sub" />
<RadioButton
android:id="@+id/multiRadio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/multi" />
<RadioButton
android:id="@+id/divRadio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/div" />
</RadioGroup>
<Button
android:id="@+id/calBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="236dp"
android:text="@string/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/num2editTxt" />
<TextView
android:id="@+id/resultViewTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/calBtn"
app:layout_constraintVertical_bias="0.245" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我可怕的片段代码失败尝试:
public class GalleryFragment extends Fragment implements View.OnClickListener {
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
final View root = inflater.inflate(R.layout.fragment_gallery, container, false);
root.findViewById(R.id.addRadio).setOnClickListener(this);
root.findViewById(R.id.subRadio).setOnClickListener(this);
root.findViewById(R.id.multiRadio).setOnClickListener(this);
root.findViewById(R.id.divRadio).setOnClickListener(this);
return root;
}
@Override
public void onClick(final View view) {
final boolean checked = ((RadioButton) view).isChecked();
View vi;
Button button = view.findViewById(R.id.calBtn);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText num1editTxt = v.findViewById(R.id.num1editTxt);
EditText num2editTxt = v.findViewById(R.id.num2editTxt);
TextView resultViewTxt = v.findViewById(R.id.resultViewTxt);
if (TextUtils.isEmpty(num1editTxt.getText().toString()) || TextUtils.isEmpty(num2editTxt.getText().toString())){
Toast.makeText(getActivity(), "Empty field not allowed!", Toast.LENGTH_LONG).show();
}
else {
int num1 = Integer.parseInt(num1editTxt.getText().toString());
int num2 = Integer.parseInt(num2editTxt.getText().toString());
int result = 0;
switch (view.getId()) {
case R.id.addRadio:
if (checked) {
result = num1 + num2;
resultViewTxt.setText(result + "");
}
break;
case R.id.subRadio:
if (checked) {
result = num1 - num2;
resultViewTxt.setText(result + "");
}
break;
case R.id.multiRadio:
if (checked) {
result = num1 * num2;
resultViewTxt.setText(result + "");
}
break;
case R.id.divRadio:
if (checked) {
result = num1 / num2;
resultViewTxt.setText(result + "");
}
break;
}
}
}
});
}
}