java – 如何使textview不可见

前端之家收集整理的这篇文章主要介绍了java – 如何使textview不可见前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的第一个 Android应用程序,所以请耐心等待,而且,如果你建议一个答案,完成任务的替代方法,请详细说明如何以及详细…

这是我的问题,经过一些研究,并尝试了,我的应用程序FC,当我有以下行(由*指定)

  1. private TextView msg;
  2.  
  3. msg = (TextView) findViewById(R.id.txtviewOut) ;
  4.  
  5. * msg.setVisibility(View.INVISIBLE);

我不知道为什么

这是我所有代码的其余部分:

main.xml中

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity$PlaceholderFragment">
  10.  
  11. <EditText
  12. android:id="@+id/textenter"
  13. android:layout_height="wrap_content"
  14. android:layout_width="match_parent"
  15. android:layout_below="@+id/lbledt1"
  16. android:layout_alignParentLeft="true" />
  17.  
  18. <TextView
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="Enter Name"
  22. android:id="@+id/lbledt1"
  23. android:layout_marginTop="26dp"
  24. android:layout_centerHorizontal="true" />
  25.  
  26. <TextView
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="Enter your date of birth (e.g. xx July 19xx)"
  30. android:id="@+id/textView"
  31. android:layout_below="@+id/textenter"
  32. android:layout_centerHorizontal="true"
  33. android:layout_marginTop="57dp" />
  34.  
  35. <EditText
  36. android:id="@+id/editText"
  37. android:layout_height="wrap_content"
  38. android:layout_width="match_parent"
  39. android:layout_below="@+id/textView"
  40. android:layout_alignParentLeft="true" />
  41.  
  42. <Button
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="Press this button"
  46. android:id="@+id/button"
  47. android:layout_centerVertical="true"
  48. android:layout_centerHorizontal="true" />
  49.  
  50. <TextView
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:textAppearance="?android:attr/textAppearanceLarge"
  54. android:text="You have been awesome since"
  55. android:id="@+id/txtviewOut"
  56. android:layout_marginTop="86dp"
  57. android:textIsSelectable="false"
  58. android:visibility="invisible"
  59. android:layout_below="@+id/button"
  60. android:layout_centerHorizontal="true" />
  61.  
  62. <TextView
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:textAppearance="?android:attr/textAppearanceLarge"
  66. android:id="@+id/txtoutName"
  67. android:textIsSelectable="false"
  68. android:layout_alignBottom="@+id/txtviewOut"
  69. android:layout_centerHorizontal="true"
  70. android:layout_marginBottom="41dp" />
  71.  
  72. <TextView
  73. android:layout_width="wrap_content"
  74. android:layout_height="wrap_content"
  75. android:textAppearance="?android:attr/textAppearanceLarge"
  76. android:id="@+id/txtOutDate"
  77. android:layout_marginTop="28dp"
  78. android:textIsSelectable="false"
  79. android:layout_below="@+id/txtviewOut"
  80. android:layout_centerHorizontal="true" />
  81.  
  82. </RelativeLayout>

这是来自MainActivity.java

  1. package com.example.helloandroidstudio;
  2.  
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.support.v7.app.ActionBar;
  5. import android.support.v4.app.Fragment;
  6. import android.os.Bundle;
  7. import android.view.LayoutInflater;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.os.Build;
  13. import android.widget.Button;
  14. import android.widget.EditText;
  15. import android.widget.TextView;
  16.  
  17. public class MainActivity extends ActionBarActivity {
  18.  
  19. private Button btnClick;
  20. private EditText Name,Date;
  21. private TextView msg,NameOut,DateOut;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.main);
  26. btnClick = (Button) findViewById(R.id.button) ;
  27. btnClick.setOnClickListener(this);
  28. Name = (EditText) findViewById(R.id.textenter) ;
  29. Date = (EditText) findViewById(R.id.editText) ;
  30. msg = (TextView) findViewById(R.id.txtviewOut) ;
  31. msg.setVisibility(View.INVISIBLE);
  32. NameOut = (TextView) findViewById(R.id.txtoutName) ;
  33. DateOut = (TextView) findViewById(R.id.txtOutDate) ;
  34. if (savedInstanceState == null) {
  35. getSupportFragmentManager().beginTransaction()
  36. .add(R.id.container,new PlaceholderFragment())
  37. .commit();
  38. }
  39. }
  40.  
  41. public void onClick(View v)
  42. {
  43. if (v == btnClick)
  44. {
  45. if (Name.equals("") == false && Date.equals("") == false)
  46. {
  47. NameOut = Name;
  48. DateOut = Date;
  49. msg.setVisibility(View.VISIBLE);
  50. }
  51. else
  52. {
  53. msg.setText("Please complete both fields");
  54. msg.setVisibility(View.VISIBLE);
  55. }
  56. }
  57. return;
  58.  
  59. }
  60.  
  61.  
  62. @Override
  63. public boolean onCreateOptionsMenu(Menu menu) {
  64.  
  65. // Inflate the menu; this adds items to the action bar if it is present.
  66. getMenuInflater().inflate(R.menu.main,menu);
  67. return true;
  68. }
  69.  
  70. @Override
  71. public boolean onOptionsItemSelected(MenuItem item) {
  72. // Handle action bar item clicks here. The action bar will
  73. // automatically handle clicks on the Home/Up button,so long
  74. // as you specify a parent activity in AndroidManifest.xml.
  75. switch (item.getItemId()) {
  76. case R.id.action_settings:
  77. return true;
  78. }
  79. return super.onOptionsItemSelected(item);
  80. }
  81.  
  82. /**
  83. * A placeholder fragment containing a simple view.
  84. */
  85. public static class PlaceholderFragment extends Fragment {
  86.  
  87. public PlaceholderFragment() {
  88. }
  89.  
  90. @Override
  91. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
  92. View rootView = inflater.inflate(R.layout.fragment_main,container,false);
  93. return rootView;
  94. }
  95. }
  96.  
  97. }

任何援助将不胜感激

解决方法

我认为它应该工作,我分析你的代码,并没有找到任何理由不工作.所以我只建议,尝试使用:
  1. to make it invisible by changing its opacity
  2. msg.setalpha(0.0f);
  3.  
  4. and to make it visible by changing its opacity
  5. msg.setalpha(1.0f);

猜你在找的Java相关文章