我真的很抱歉,因为我在很多类似的问题(我猜你知道)已经存在之后写这篇文章,但我需要你的帮助。我尝试了其他帖子中描述的不同方法,但没有一个适合我。我是Android开发的新手,我有一个简单的'#Hello World!"应用程序。我用自定义材料设计主题和按钮定制它,也定制。我试图使用" onClick"单击按钮时显示TextView的功能,但我不知道如何操作。这是我的相对布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/helloworld1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:text="@string/hello_world"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/press_here"
android:textColor="@color/White"
android:background="@color/Red"
android:translationY="40dp"/>
这是我的MainActivity.java:
package com.giuskiller.material;
import android.app.*;
import android.os.*;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
答案 0 :(得分:0)
您需要定义一个显示TextView
的方法,然后将该方法分配给{{1}的onClick
属性像这样:
布局XML:
Button
的活动:
<TextView
android:id="@+id/helloworld1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:text="@string/hello_world"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/press_here"
android:textColor="@color/White"
android:onClick="showTextView"
android:background="@color/Red"
android:translationY="40dp"/>