2013-11-23 10 views
5

To jest moja pierwsza aplikacja na Androida, więc proszę o mnie, a także, jeśli sugerujesz odpowiedź, alternatywną metodę wykonania zadania, proszę określić, jak i szczegółowo ...jak zrobić niewidoczny widok tekstowy

Oto mój problem, po robi rozeznanie i próbuje ją, moja aplikacja FC kiedy mam następujący wiersz (określony przez *)

private TextView msg; 

msg = (TextView) findViewById(R.id.txtviewOut) ; 

* msg.setVisibility(View.INVISIBLE); 

nie mam pojęcia dlaczego

tutaj jest reszta całego mojego kodu:

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> 

i tu jest z 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; 
     } 
    } 

} 

Każda pomoc będzie mile widziane

+0

gdzie jest twój tekstView with id txtviewOut in layout ?? – Manishika

+0

@Manishika on ma. sprawdź w xml ... –

+0

Dodaj fatalne dzienniki wyjątków .. –

Odpowiedz

5

myślę, że powinno działać, i przeanalizował swój kod i nie znaleziono jakikolwiek powód, aby nie pracować. więc sugeruję tylko, że spróbuj użyć:

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); 
8

spróbuj ustawić w xml

android:visibility="invisible" 

aw swojej działalności, w którym ma on być widoczny

object of texview.setvisibility(View.VISIBLE); 
0

android:visibility="gone" jest lepsza niż tylko invisible, ponieważ gdy jest on niewidoczny TIP-tekst jest wyświetlany w każdym razie . Kiedy "zniknie", Android przenosi inne elementy do miejsca "minionego" elementu, a widok jest piękniejszy. Oczywiście zależy od przypadku.

Powiązane problemy