如何使按钮上的TextView可见Click()?

时间:2015-03-23 12:48:27

标签: android

我想要它,这样当我点击一个按钮时,文字就会变得可见 我失败了。

请帮帮我

Loveandriod.java

    package com.example.farjad.andriodlove;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class LoveAndriod extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_love_andriod);
    }
    public void onLoveButtonClicked(View view){
      TextView.setVisibility(R.id.haikowtext);
        TextView haikotext=findViewById(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.menu_love_andriod, 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.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

,活动日志如下;

activity.love_andriod.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=".LoveAndriod">

    <TextView android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/haikowtext"

        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="123dp"
        android:visibility="invisible" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/love_button_text"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="onLoveButtonClicked"/>

</RelativeLayout>

我怎么能这样做呢? 我是Android Studio的初学者。

2 个答案:

答案 0 :(得分:0)

试试这个:

TextView haikotext = (TextView)findViewById(R.id.haikowtext);    
haikotext.setVisibility(View.VISIBLE);

答案 1 :(得分:0)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_love_andriod);
        TextView tv = (TextView)findViewById(R.id.haikowtext); 

    }

public void onLoveButtonClicked(View view){
  tv.setVisibility(View.VISIBLE);

}