尝试显示editText时出错

时间:2012-02-24 01:42:03

标签: android android-edittext

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

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/Text1" />


<CheckBox
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/check"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
  />

 <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/Text2" />

 <RadioGroup
    android:id="@+id/rg"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <RadioButton android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Mazda" />
 <RadioButton android:id="@+id/radio2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Toyota" />
 <RadioButton android:id="@+id/radio3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Nissan" />
 <RadioButton android:id="@+id/radio4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hyundai" />
   </RadioGroup>

   <EditText
       android:inputType="text|textMultiLine"
android:id="@+id/etext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></EditText>



 <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

</LinearLayout>

   package nidhin.survey;

import android.app.Activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class SurveyActivity extends Activity implements OnCheckedChangeListener

{
CheckBox cb;
String myChoice;

RadioButton radio1;
RadioButton radio2;
RadioButton radio3;
RadioButton radio4;
RadioGroup rg;
EditText text1;

public void onCreate(Bundle savedInstanceState) 

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    cb=(CheckBox)findViewById(R.id.check);
    cb.setOnCheckedChangeListener(this);
    rg=(RadioGroup)findViewById(R.id.rg);
    radio1=(RadioButton)findViewById(R.id.radio1);
    radio2=(RadioButton)findViewById(R.id.radio2);
    radio3=(RadioButton)findViewById(R.id.radio3);
    radio4=(RadioButton)findViewById(R.id.radio4);
    text1=(EditText)findViewById(R.id.etext1);
    text1.setText(myChoice);
}



  cb=(CheckBox)findViewById(R.id.check);
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView,
       boolean isChecked) 
       {
               if (isChecked)
               {
                cb.setText("This checkbox is: checked");
               }
               else
               {
                cb.setText("This checkbox is: unchecked");
               }
       }   
    }
    );


    setContentView(R.layout.main);

    Button1=(Button)findViewById(R.id.button); // through XML file
    Button1.setText("Click to display updated time");


    Button2=(Button)findViewById(R.id.button2);
    Button2.setText("Not Pressed yet");

    Button3=(Button)findViewById(R.id.button3);
    Button3.setText("Press for image");

}

public void myButton1 (View view)
{
updateTime();
}

public void myButton2 (View view )
{

Button2.setText("Pressed");
}

public void myButton3 (View view)
{
    image = (ImageView)findViewById(R.id.image);
image.setVisibility(View.VISIBLE);//image.setVisibility(1);
}



private void updateTime()
{
    Button1.setText(new Date().toString());
}
}

我有一个显示复选框,radiogroup和editText框的程序。但问题是编辑文本框没有显示。 CheckedBox和单选按钮显示但不显示编辑文本框。在main.xml文件中,如果将editText框放在radiogroup上方,则程序崩溃。编辑文本未正确声明?

2 个答案:

答案 0 :(得分:1)

RadioGroup atts android:layout_heightfill_parent,请更改为wrap_content

答案 1 :(得分:1)

请将您的Radiogroup属性更改为

         <RadioGroup
android:id="@+id/rg"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>

您用作layout_height属性的fill_parent。