帮助使多个文本字段可见或不可见

时间:2015-09-23 02:20:21

标签: android android-studio

我正在介绍Android Studio课程,为了完成家庭作业,我们必须构建一个几何形状应用程序。原理类似于我们在之前的任务中所做的,期望在这个任务中我们有多个形状和字段。为了让事情变得更具挑战性,我们的老师投入曲线,他说他希望我们在每个形状上显示适当的字段。所以

圆圈将显示区域和周长 球体将显示体积和表面积

但是,形状不会显示与每个形状无关的信息。我们得到了两个提示

1。)b.setVisibility(View.GONE); //使按钮消失 2.)使用Google解决任何其他项目

我当前的问题是:当用户选择圆形等形状时,它会要求他们输入所有内容:

  • 半径
  • 高度
  • 长度

然后当他们点击“查找音量”时,我的输出会显示所有内容

  • 区域
  • 体积
  • 表面积
  • 等....

我有一个按钮设置

    Button b = (Button) findViewById(R.id.buttonFindVolume);
    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //find out type of shape selected by the user
            Spinner sp = (Spinner) findViewById(R.id.spinnerShapes);
            shapeType = sp.getSelectedItem().toString();

            //Extract the radius from the radius field
            EditText radiusText = (EditText) findViewById(R.id.editTextRadius);
            radius = Double.parseDouble(radiusText.getText().toString());

            //Extract the height from the height field
            EditText heightText = (EditText) findViewById(R.id.editTextHeight);
            height = Double.parseDouble(heightText.getText().toString());

            //Extract the length from the length field
            EditText lengthText = (EditText) findViewById(R.id.editTextLength);
            length = Double.parseDouble(lengthText.getText().toString());

与IF语句一起计算形状体积

if (shapeType.equalsIgnoreCase("Circle")) {

                    //compute area and circumference of the circle
                    area = (PI * radius * radius);
                    circumference = (2 * PI) * (radius);

                    //pack circle area and circumference into intent
                    intent.putExtra("computedArea", area);
                    intent.putExtra("competedCircumference", circumference);

                    //start the target activity - VolumeDisplay
                    startActivity(intent);

            } else if (shapeType.equalsIgnoreCase("Cylinder")) {

                //compute volume of the cylinder
                volume = PI * radius * radius * height;

                //pack cylinder volume into intent for the volume display activity
                intent.putExtra("computedVolume", volume);

                //start the target activity - VolumeDisplay
                startActivity(intent);

我的问题是我如何使用set.visibility以及当用户选择形状时,我们应该如何在程序开始时实现它们,以及在显示信息时最终实现它们。

谢谢

2 个答案:

答案 0 :(得分:0)

我建议你做这样的事情:

在xml布局中隐藏除微调器之外的所有项目。

 android:visibility="gone"

然后将一个onItemSelectedlistener添加到您的微调器,如下所示:

sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

    shapeType =  parentView.getItemAtPosition(position).toString();
    if(shapeType.equalsIgnoreCase("Circle")){
    // your code for set visibility to edittext here.
    }
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {

}
});

然后在onItemSelectedListener中你应该设置Visivility(View.VISIBLE)或View.GONE,具体取决于你应该为每个形状显示的字段,例如:

如果选中的微调器项目是圆圈:

 radiusText.setVisivility(View.VISIBLE);

此外,如果你想做一个好的作业你可以添加到editText,如果edittext有一些文字,你可以显示/隐藏或启用/禁用提交按钮。

radiusText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {

}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (s.lenght > 0) {
        b.setVisivilty(View.VISIBLE);
    } else {
        b.setVisivilty(View.GONE);
    }
}
});

  b.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
     if(shapeType.equalsIgnoreCase("Circle")){
                area = (PI * radius * radius);
                circumference = (2 * PI) * (radius);

                //pack circle area and circumference into intent
                intent.putExtra("computedArea", area);
                intent.putExtra("competedCircumference", circumference);
          }else if(shapeType ..) {// THE OTHER ITEMS

          }

          startActivity(intent);
    });

答案 1 :(得分:0)

直接在radioGroup下创建一个RadioGroup而不是TextFields的形状按钮 在EditText的

上面的xml中添加它
 <RadioGroup   
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"    
            android:id="@+id/shapes"    
            android:background="#abf234"    
            android:checkedButton="@+id/circle" >

            <RadioButton    
                android:layout_width="wrap_content"   
                android:layout_height="wrap_content" 
                android:id="@+id/cylinder"
                android:text="@string/Cylinder" />

            <RadioButton
                android:layout_width="wrap_content"    
                android:layout_height="wrap_content"    
                android:id="@+id/sphere"    
                android:text="@string/Sphere" />   

            <RadioButton    
                android:layout_width="wrap_content"    
                android:layout_height="wrap_content"    
                android:id="@+id/circle"   
                android:text="@string/Circle" />

        </RadioGroup>

onCreate()中声明所有必填字段,并根据形状使其可见/不可见,以便用户只能输入所需的值

     EditText radiusText = (EditText) findViewById(R.id.editTextRadius);
     EditText heightText = (EditText) findViewById(R.id.editTextHeight);
     EditText lengthText = (EditText) findViewById(R.id.editTextLength);     
     RadioGroup radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);

        //initailly check item is circle so display fields for cycle
         lengthText.setVisible(View.GONE);
         radiusText.setVisible(View.VISIBLE);
         heightText.setVisible(View.GONE);
          radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

               @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // find which radio button is selected
                    if(checkedId == R.id.circle) {
                         //make circle fields visible and others invisible 
                    } else if(checkedId == R.id.cylinder) {
                        //make cylinder fields visible and others invisible
                    } else {
                         //make sphere fields visible and others invisible
                    }
                }            
            });

//this will be common submit button instead of shapes buttons
        submitbtn.setonClickListener(new View.onClickListener(){
        @Override
        public void onClick()
        {
         int radioButtonID = radioGroup.getCheckedRadioButtonId();
          View radioButton = radioGroup.findViewById(radioButtonID);
         int checkedid= radioGroup.indexOfChild(radioButton);
         if(checkedid==0)
         {
          //sphere calculations
           }
          else if(checkedid==1)
          {
          //cylinder calculations
          }
         else{
         //circle calculations 
          }
        } 
        });
相关问题