【问题标题】:Click using RadioButton使用 RadioButton 单击
【发布时间】:2021-12-05 05:22:17
【问题描述】:
fragment2_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:id="@+id/G1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/R1"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="By Specialist" />

        <RadioButton
            android:id="@+id/R2"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="By Doctor"/>
    </RadioGroup>


</LinearLayout>

Fragment2.java
package com.example.tabbedapp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

public class Fragment2 extends Fragment {
    public Fragment2(){

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment2_layout, container, false);

        Intent intent = new Intent(getActivity(), Specialist.class);
        RadioButton button = (RadioButton) rootView.findViewById(R.id.R1);
        button.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                startActivity(intent);
            }
        });
        return rootView;
    }

}

点击 R1 我需要移动到 Specialist.class 页面,点击 R2 我需要移动到 Doctor.class 页面。如何在java文件中实现代码?我在单击 RadioButton 时实现了上述代码,它会关闭应用程序。请问谁能解决我的问题? Intent 中是否有任何错误。 enter image description here

【问题讨论】:

  • 你能分享错误信息吗?还有(按钮)rootView.findViewById(R.id.R1);您应该将 (Button) 替换为 RadioButton
  • 我附上了显示错误的图片。我已经用 RadioButton 替换了 Button。
  • 你的 AndroidManifest.xml 中有代码,允许你的活动吗?前任。
  • 是的,在 AndroidManifest.xml 中存在活动。

标签: android


【解决方案1】:

你可以这样使用

button.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(this,  
            Specialist.class);
        }

或者

final RadioGroup group= (RadioGroup) findViewById(R.id.radioGroupId);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
    int id = group.getCheckedRadioButtonId();
    switch (id) {
        case R.id.r1:
            // Your code
            break;
        case R.id.r2:
            // Your code
            break;
        case R.id.r3:
            // Your code
            break;
        case R.id.r4:
            // Your code
            break;
        default:
            // Your code
            break;
    }
}});

【讨论】:

  • 此代码对我不起作用。应用程序未打开。
【解决方案2】:

我希望,您的诸如 Speacialist 和 Doctor 之类的课程是活动,而不是片段,所以您可能忘记在清单中注册您的活动。

   <activity
        android:name=".Specialist"/>
   <activity
        android:name=".Doctor"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 2018-04-10
    • 2014-09-30
    • 2014-07-09
    相关资源
    最近更新 更多