eclipse 如何设置editText可见/不可见?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32682103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set editText visible/invisible?
提问by Ashraf96
In my program I want to be able to hide the edit text when a radio button is check and then reappear when the user clicks the other radio button.
在我的程序中,我希望能够在选中单选按钮时隐藏编辑文本,然后在用户单击另一个单选按钮时重新出现。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waist2height); {
final EditText h = (EditText)findViewById(R.id.editText2);
final RadioButton rCM = (RadioButton) findViewById(R.id.radioCM);
final RadioButton rFT = (RadioButton) findViewById(R.id.radioFT);
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}}
The code below is where i have the problem i only added in the relevant part of my code instead of the entire thing
下面的代码是我遇到问题的地方,我只在代码的相关部分而不是整个事情中添加了
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}
Unfortunately I can't seem to get it to work.
Am I missing anything? Or have I got it all wrong altogether?
I have tried h.setVisibility(View.GONE);
however it just ruines the format of the xml.
不幸的是,我似乎无法让它发挥作用。我错过了什么吗?还是我完全弄错了?我已经尝试过,h.setVisibility(View.GONE);
但它只会破坏 xml 的格式。
采纳答案by Skizo-oz??S
I've made a sample and it works for me just take a look at this code :
我制作了一个示例,它对我有用,只需看一下这段代码:
I've declared those variables as global
我已将这些变量声明为全局变量
RadioButton rb1,rb2;
TextView tbHideOrNot;
Then in my onCreate()
I've got this :
然后在我的onCreate()
我有这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb1 = (RadioButton)findViewById(R.id.rb1);
rb2 = (RadioButton)findViewById(R.id.rb2);
tbHideOrNot = (TextView)findViewById(R.id.textView);
rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tbHideOrNot.setVisibility(View.INVISIBLE);
rb2.setChecked(false);
}
}
});
rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tbHideOrNot.setVisibility(View.VISIBLE);
rb1.setChecked(false);
}
}
});
}
In my opinion, that's not a good idea use a RadioButton
for the use that you want, I recommend you to use CheckBox
cause you can only click ONEand this you have to check if the first RadioButton
is clicked and you press the second one you'll have to uncheck the first, etc... well It's your code I'm only giving to you an advice, this code is working for me, let me know if it works for you :)
在我看来,这不是一个好主意,将 aRadioButton
用于您想要的用途,我建议您使用,CheckBox
因为您只能单击ONE并且您必须检查是否RadioButton
单击了第一个,然后按下第二个您将拥有取消选中第一个等等......好吧这是你的代码我只是给你一个建议,这个代码对我有用,让我知道它是否适合你:)
By the way if you want to use this code :
顺便说一句,如果您想使用此代码:
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}
It's fine but you only will hide or not the TextView
when you start the Activity
and you'll have to put any RadioButton
(if you want) android:checked="true"
or false
if you want to be showed or not the TextView
, but with this code you won't get the click event on the RadioButtons
.
没关系,但是您只会TextView
在开始时隐藏或不隐藏Activity
并且您必须放置任何RadioButton
(如果需要)android:checked="true"
或者false
是否要显示TextView
,但是使用此代码您将不会获得点击事件在RadioButtons
.
回答by rajan ks
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==0){
h.setVisibility(View.VISIBLE);
}
else if(checkedId==1){
h.setVisibility(View.INVISIBLE);
}
}
});
回答by Rutvik
Try this :
尝试这个 :
rCM.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
h.setVisibility(View.VISIBLE);
}
});
rFT.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
h.setVisibility(View.INVISIBLE);
}
});