gpt4 book ai didi

android - 如何设置 android.widget.textview 的可见性选项

转载 作者:太空狗 更新时间:2023-10-29 15:41:56 25 4
gpt4 key购买 nike

FC 对此有问题

private TextView msg, NameOut, DateOut;
msg = (TextView) findViewById(R.id.txtviewOut) ;

线下...(问题出在这里)

msg.setVisibility(View.INVISIBLE);//set visibility to false on create

如何在应用程序启动时将此 TextView (msg) 的可见性设置为 false?

#1 答案编辑后更新

好的,这是所有的代码:

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;
}
}

}

我用setInvisibility(View.GONE)试过了,还是FC

我检查了 id 值,分配了一个辅助值,我确实对对象进行了类型转换...

请记住,我已经完成了 c#,但这是我的第一个 java/android 应用程序,所以请多多包涵

最佳答案

好吧,我有点怀疑你到底想完成什么,但我会试着澄清一些概念

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

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

关于android - 如何设置 android.widget.textview 的可见性选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20155051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com