【问题标题】:RadioButtonGroup are not showing upRadioButtonGroup 没有出现
【发布时间】:2013-12-19 09:19:17
【问题描述】:

在您的帮助下成功解决按钮后,我遇到了一个问题,RadioButtonGroup 没有出现。

这是代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android2="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="10"
        >

        <TextView
            android:id="@+id/secondLine"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/Greetings"
            android:textSize="20sp"
            />

        <Button
            android:id="@+id/Button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="HelloWord"
            />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android2:baselineAligned="_baseline"
        android:orientation="horizontal"
        android:weightSum="10"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3.3"
            android:background="#ff0000"
            android:gravity="center"
            android:text="red"
            android:textColor="#000000"
            />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="3.4"
            android:background="#00ff00"
            android:gravity="center"
            android:text="green"
            android:textColor="#000000"

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="3.3"
            android:background="#0000ff"
            android:gravity="center"
            android:text="blue"
            android:textColor="#000000"

    </LinearLayout>

    <RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

    <RadioButton
        android:id="@+id/myRadioButtonRed"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weight="1"
        />

    <RadioButton
        android:id="@+id/myRadioButtonGreen"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weight="1"
        />

    <RadioButton
        android:id="@+id/myRadioButtonBlue"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weight="1"
        />

</LinearLayout>

注意:我有一个错误说:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="3.3"
    android:background="#0000ff"
    android:gravity="center"
    android:text="blue"
    android:textColor="#000000"
    />

后面必须跟一个属性说明。

希望能得到帮助,谢谢。

【问题讨论】:

    标签: java android textview android-radiogroup


    【解决方案1】:

    应该是这样的

    button1.setOnClickListener(new OnClickListener()
    {
      public void onClick(View v)
      {
         //stuff
      }
    });
    
    button2.setOnClickListener(new OnClickListener()
    {
      public void onClick(View v)
      {
         // stuff
      }
    });
    

    【讨论】:

      【解决方案2】:
      1. 没有Button.OnClickListener 类。请改用View.OnClickListener。替换

        Button.OnClickListener myListener = new Button.OnClickListener()
        

        与

        View.OnClickListener myListener = new View.OnClickListener()
        
      2. 一个类只能有一个具有相同签名的方法。删除另一个 onClick(View) 定义。

      【讨论】:

      • 我的问题还是最后一句话
      【解决方案3】:

      就这样用吧

                  @Override
                  public void onCreate(Bundle savedInstanceState) 
                  {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.activity_main);
      
                      Button Button1=(Button)findViewById(R.id.Button1);
                      Button1.setOnClickListener(mClickListener);
                  }
      
                  View.OnClickListener mClickListener =new OnClickListener()
                  {           
                      @Override
                      public void onClick(View v) {
                      // write your code here
      
                      }
                  };
      

      【讨论】:

        【解决方案4】:

        它应该像这样实现按钮(Onclicklistener)。检查下面的代码

         Button btn;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn = (Button) findViewById(R.id.button1);
        
            btn.setOnClickListener(listener);
        
        }
        
        View.OnClickListener listener = new View.OnClickListener() {
        
            @Override
            public void onClick(View v) {
                if (v == btn) {
                    Toast.makeText(MainActivity.this, "test", Toast.LENGTH_LONG)
                            .show();
                }
            }
        };
        

        【讨论】:

          【解决方案5】:

          你也可以这样用……

          Button Button1=(Button)findViewById(R.id.Button1);
          Button Button2=(Button)findViewById(R.id.Button2);
              View.OnClickListener myListener = new View.OnClickListener(){
          
                  @Override
                  public void onClick(View v) {
                  if(view.getId()==R.id.Button1){
                      Toast.makeText(Nasser.this, "Button1", Toast.LENGTH_LONG).show();
                  }
                  else if(view.getId()==R.id.Button2){
                      Toast.makeText(Nasser.this, "Button2", Toast.LENGTH_LONG).show();
                  }
          
              };
              button1.setOnClickListener(myListener);
              button2.setOnClickListener(myListener);
          

          【讨论】:

            【解决方案6】:

            您收到该错误是因为您的 XML 格式不正确。您已经为您的两个 TextViews、您的 RadioGroup 省略了结束标签,并且还有一个 LinearLayout 的额外结束标签。还有一个未知属性 android2:baselineAligned="_baseline" 在您的 linearLayout2 中。用附加的代码替换您的 XML 代码。你不应该得到你所面临的错误。另外,我已经修复了 RadioButtons 的宽度。当您希望它们都在同一行时,它们不应该是 fill_parent。我也知道您希望每个 RadioButton 占据屏幕宽度的 1/3。这是作为练习而忽略的。

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:android2="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            
                <LinearLayout
                    android:id="@+id/linearLayout1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:weightSum="10" >
            
                    <TextView
                        android:id="@+id/secondLine"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:textSize="20sp"
                        android:text="@string/Greetings" />
            
                    <Button
                        android:id="@+id/Button1"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="HelloWord" />
                </LinearLayout>
            
                <LinearLayout
                    android:id="@+id/linearLayout2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="horizontal"
                    android:weightSum="10">
            
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="3.3"
                        android:background="#ff0000"
                        android:gravity="center"
                        android:text="red"
                        android:textColor="#000000" />
            
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="3.4"
                        android:gravity="center"
                        android:background="#00ff00"
                        android:text="green"
                        android:textColor="#000000"/>
            
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="3.3"
                        android:gravity="center"
                        android:background="#0000ff"
                        android:text="blue"
                        android:textColor="#000000"/>
            
                </LinearLayout>
            
                <RadioGroup
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/myRadioGroup"
                    android:orientation="horizontal">
            
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:weight="1"
                    android:id="@+id/myRadioButtonRed" />
            
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:weight="1"
                    android:id="@+id/myRadioButtonGreen" />
            
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:weight="1"
                    android:id="@+id/myRadioButtonBlue" />
                </RadioGroup>
            
            </LinearLayout>
            

            【讨论】:

            • 我刚刚注意到,这是一个 4 年半的帖子!不要认为现在的答案是相关的,但仍然把它留在这里,因为没有一个答案是正确的。而且我已经格式化了答案,而没有查看被问到的日期。
            猜你喜欢
            • 2010-11-18
            • 2021-12-12
            • 1970-01-01
            • 2021-02-11
            • 2015-01-19
            • 2012-09-21
            • 2021-02-15
            • 2014-05-31
            • 2013-12-02
            相关资源
            最近更新 更多