Java 如何使文本视图不可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20159969/
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 make a textview invisible
提问by
this is my first android app so plese bear with me, and also, if you suggest a answer, alternative method of completeing a task, please specify how and also in detail...
这是我的第一个 android 应用程序,所以请多多包涵,而且,如果您建议一个答案,完成任务的替代方法,请详细说明如何以及详细...
Here is my problem, after doing some research, and trying it out, my app FC when I have the following line (specified by *)
这是我的问题,经过一些研究和尝试后,当我有以下行(由 * 指定)时,我的应用程序 FC
private TextView msg;
msg = (TextView) findViewById(R.id.txtviewOut) ;
* msg.setVisibility(View.INVISIBLE);
I have no idea why
我不知道为什么
here is the rest of all my code:
这是我所有代码的其余部分:
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:visibility="invisible"
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" />
</RelativeLayout>
and here is from MainActivity.java
这是来自 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) ;
btnClick.setOnClickListener(this);
Name = (EditText) findViewById(R.id.textenter) ;
Date = (EditText) findViewById(R.id.editText) ;
msg = (TextView) findViewById(R.id.txtviewOut) ;
msg.setVisibility(View.INVISIBLE);
NameOut = (TextView) findViewById(R.id.txtoutName) ;
DateOut = (TextView) findViewById(R.id.txtOutDate) ;
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);
}
}
return;
}
@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;
}
}
}
Any assistance would be appreciated
任何援助将不胜感激
采纳答案by Hamad
i think it should work,i analysed your code and didn't found any reason to not work.so i only suggest that,try to use:
我认为它应该可以工作,我分析了您的代码并没有发现任何不工作的原因。所以我只建议,尝试使用:
to make it invisible by changing its opacity
msg.setalpha(0.0f);
and to make it visible by changing its opacity
msg.setalpha(1.0f);
回答by Pankaj Arora
try to set in xml
尝试在 xml 中设置
android:visibility="invisible"
and in your activity where you want it to be visible
并在您希望它可见的活动中
object of texview.setvisibility(View.VISIBLE);
回答by Grafello
android:visibility="gone"
is better than just invisible
, because
while it is invisible HINT-text is shown anyway. When it is "gone" Android moves other elements to the "gone" element place and all view is more beautiful. Of course depends on case.
android:visibility="gone"
比只是更好invisible
,因为虽然它是不可见的,但无论如何都会显示提示文本。当它“消失”时,Android 将其他元素移动到“消失”元素的位置,所有视图都更漂亮。当然要看情况。