如何设置 android.widget.textview 的可见性选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20155051/
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 visibility options of android.widget.textview
提问by Rich
had FC issues with this
对此有 FC 问题
private TextView msg, NameOut, DateOut;
msg = (TextView) findViewById(R.id.txtviewOut) ;
down the line... (problem lies here)
下线......(问题出在这里)
msg.setVisibility(View.INVISIBLE);//set visibility to false on create
how do I set the visibility of this TextView (msg) to false when the app starts?
如何在应用程序启动时将此 TextView (msg) 的可见性设置为 false?
UPDATE AFTER #1 Answer EDIT
更新后 #1 答案编辑
okay here is all the code:
好的,这里是所有代码:
fragment_main.xml
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">
<EditText
android:id="@+id/textenter"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/lbledt1"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Name"
android:id="@+id/lbledt1"
android:layout_marginTop="26dp"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter your date of birth (e.g. xx July 19xx)"
android:id="@+id/textView"
android:layout_below="@+id/textenter"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp" />
<EditText
android:id="@+id/editText"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press this button"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="You have been awesome since"
android:id="@+id/txtviewOut"
android:layout_marginTop="86dp"
android:textIsSelectable="false"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtoutName"
android:textIsSelectable="false"
android:layout_alignBottom="@+id/txtviewOut"
android:layout_centerHorizontal="true"
android:layout_marginBottom="41dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtOutDate"
android:layout_marginTop="28dp"
android:textIsSelectable="false"
android:layout_below="@+id/txtviewOut"
android:layout_centerHorizontal="true" />
MainActivity.java:
package com.example.helloandroidstudio;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private Button btnClick;
private EditText Name, Date;
private TextView msg, NameOut, DateOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnClick = (Button) findViewById(R.id.button) ;
Name = (EditText) findViewById(R.id.textenter) ;
Date = (EditText) findViewById(R.id.editText) ;
msg = (TextView) findViewById(R.id.txtviewOut) ;
NameOut = (TextView) findViewById(R.id.txtoutName) ;
DateOut = (TextView) findViewById(R.id.txtOutDate) ;
// msg.setVisibility(View.GONE);//set visibility to false on create
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void onClick(View v)
{
if (v == btnClick)
{
if (Name.equals("") == false && Date.equals("") == false)
{
NameOut = Name;
DateOut = Date;
msg.setVisibility(View.VISIBLE);
}
else
{
msg.setText("Please complete both fields");
msg.setVisibility(View.VISIBLE);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
I tried it with setInvisibility(View.GONE), but still FC
我用 setInvisibility(View.GONE) 试过了,但还是 FC
I check witht the id values, there isa aid value assisgned and I did do the typecast of the object...
我检查了 id 值,有一个辅助值分配,我确实做了对象的类型转换......
please keep in mind, I have done c#, but this is my first java/android app so please bear with me
请记住,我已经完成了 c#,但这是我的第一个 java/android 应用程序,所以请耐心等待
回答by Mehroz Munir
well i have some doubt what exactly are you trying to accomplish , but i would try to clear some concept
好吧,我有点怀疑你到底想完成什么,但我会尝试澄清一些概念
if you want to hide your view completely then use Visibility.Gone or if you want to hide it but want to keep the place then use Visibility.Invisible
如果你想完全隐藏你的视图然后使用 Visibility.Gone 或者如果你想隐藏它但想要保留这个地方然后使用 Visibility.Invisible
msg.setVisibility(View.INVISIBLE); // for hiding but keeping the place of your view
msg.setVisibility(View.GONE);// for hiding it completely , view will not take any place
回答by Rich
Set that as the default in your layout.
将其设置为布局中的默认值。
<TextView
android:id="@+id/txtviewOut"
...
android:visibility="invisible" />
UPDATE:
更新:
If you always need it to start off invisible, this is the way to go. Note that "invisible" means that it's not displayed but it will still take up blank space when rendering. If you want it to be collapsed (does not take up any space, does not display), then use View.GONE
in code or android:visibility="gone"
in your layouts.
如果你总是需要它开始隐形,这是要走的路。请注意,“不可见”意味着它不显示,但在渲染时仍会占用空白空间。如果您希望它折叠起来(不占用任何空间,不显示),请View.GONE
在代码或android:visibility="gone"
布局中使用。
If calling setVisibility on a View causes a FC, that's most likely going to be a null pointer exception (check in LogCat though to be sure). If that's the case, you either haven't called findViewById yet or your layout does not contain the view by that id.
如果在视图上调用 setVisibility 导致 FC,那很可能是空指针异常(但请检查 LogCat 以确保)。如果是这种情况,您要么尚未调用 findViewById,要么您的布局不包含该 id 的视图。
UPDATE #2:
更新#2:
First of all, you should always look at LogCat and get the exact exception that is thrown and the exact line it is thrown on. I'm willing to bet it happens on this line:
首先,您应该始终查看 LogCat 并获取抛出的确切异常以及抛出的确切行。我敢打赌它发生在这条线上:
NameOut = Name;
DateOut = Date;
You're attempting to set a variable of type TextView to a value of type EditText. What I assume you're trying to do is set the text of your TextView to the value of the text in your EditText. This is done as follows:
您正在尝试将 TextView 类型的变量设置为 EditText 类型的值。我假设您要做的是将 TextView 的文本设置为 EditText 中文本的值。这是按如下方式完成的:
NameOut.setText(Name.getText().toString());
DateOut.setText(Date.getText().toString());
回答by Harsh Singal
You have implemented an
你已经实施了
onClick(View v)
method but did not wire up with your Button in the layout.
方法,但没有与布局中的 Button 连接。
Set the property android:onClick="onClick"
in your layout xml file.
android:onClick="onClick"
在布局 xml 文件中设置属性。
Right now, your button click is not getting registered and the method is not getting executed
现在,您的按钮单击未注册,并且该方法未执行
回答by DikShU
For Doing this in Kotlin:
在Kotlin 中这样做:
<Text android:visibility="gone"/>
If you always need it to start off invisible, this is the way to go. Note that "invisible" means that it's not displayed but it will still take up blank space when rendering. If you want it to be collapsed (does not take up any space, does not display), then use View.GONE
in code.
如果你总是需要它开始隐形,这是要走的路。请注意,“ invisible”表示不显示,但在渲染时仍会占用空白空间。如果你想让它折叠起来(不占用任何空间,不显示),那么View.GONE
在代码中使用。
For Visibility:
可见性:
text.Visibility = View.Visible
For Gone :
对于消失:
text.Visibility = View.Gone
(As Gone does not uses your space in main_activity.xml
.
It Consumes Space Only When it is Set To Visible.)
(因为 Gone 不会使用您的空间main_activity.xml
。它仅在设置为可见时消耗空间。)