开发者

Bring a view on selecting one view

开发者 https://www.devze.com 2023-01-27 22:22 出处:网络
hii i want 开发者_运维问答to create a ui in which as i select a radio button there should come a textview.

hii i want 开发者_运维问答to create a ui in which as i select a radio button there should come a textview. when that button is not selected text view should not be visible. and as button got selected it should be visible..can i implement this??


Inside the listener where you check if the radio button is selected or not:

 findViewById(R.id.yourtextview).setVisibility(View.INVISIBLE);

and

 findViewById(R.id.yourtextview).setVisibility(View.VISIBLE);

You can choose between INVISIBLE and GONE.

Tutorial to manage radio buttons


Here is sample codes for you...

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

    private RadioButton radioButton1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



        radioButton1 = (RadioButton) findViewById(R.id.RadioButton1);


        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(radioButton1.isChecked()) {
                    findViewById(R.id.textView).setVisibility(View.VISIBLE);
                } else {
                    findViewById(R.id.textView).setVisibility(View.GONE);
                }
            }
        });
    }
}

And here is the xml layout: Main.xml

Hope it will help you a lot...


public class _alefon_radio extends Activity implements OnCheckedChangeListener {
 /** Called when the activity is first created. */
 private TextView tx;
 private RadioGroup rg;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  tx = (TextView) findViewById(R.id.tvv);
  rg = (RadioGroup) findViewById(R.id.rgroup);
  rg.setOnCheckedChangeListener(this);
 }

 @Override
 public void onCheckedChanged(RadioGroup group, int checkedId) {

  switch (checkedId) {
  // for R.id.option1
  case R.id.option1:
   tx.setText("option one is checked");
   //tx.setVisibility(0); //visible 
   break;
  default:
   tx.setText("");
   //tx.setVisibility(4); //invisible

  }

 }
}

and layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
 android:id="@+id/tvv"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text=""
    />
    <RadioGroup
    android:id="@+id/rgroup"
  android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">
   <RadioButton
    android:id="@+id/option1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 1" />
   <RadioButton
    android:id="@+id/option2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 2" />
   <RadioButton
    android:id="@+id/option3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 3" />
 </RadioGroup>
</LinearLayout>

I used edit text instead of setting visbile/invisible but also I included visibilty controls (commented) if you`d like to use this way.

I hope this is what you are looking for.

good luck,

0

精彩评论

暂无评论...
验证码 换一张
取 消