【发布时间】:2016-05-03 13:32:45
【问题描述】:
我的 XML 文件中有一个相对布局,它包含一个按钮。现在我希望当我按下这个按钮时,它会创建 2 个 TextView。请帮忙,因为我是 Android Studio 的新手?我已经尝试为按钮创建 onClickListener,但我遇到了问题,以便获取我在 XML 中拥有的当前相对布局的对象。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brush_your_teeth);
Intent i = getIntent();
final Button addAlertButton = (Button)findViewById(R.id.AddAlert);
addAlertButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
}
});
}
以下是 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.dentalapp.BrushYourTeeth"
tools:showIn="@layout/activity_main">
<!--ALERT 1-->
<TextView
android:id="@+id/Alert1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert 1"
android:textSize="25dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"/>
<TextView
android:id="@+id/Time1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="08:00"
android:textSize="25dp"
android:onClick="showTimePickerDialog"
android:layout_above="@+id/Alert2"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"/>
<!--ALERT 2-->
<TextView
android:id="@+id/Alert2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert 2"
android:textSize="25dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_below="@id/Alert1"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"/>
<TextView
android:id="@+id/Time2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="21:00"
android:textSize="25dp"
android:layout_below="@id/Alert1"
android:layout_marginTop="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"/>
<!--ADD ALERT BUTTON-->
<Button
android:id="@+id/AddAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Alert"
android:textAllCaps="false"
android:textSize="25dp"
android:padding="15dp"
android:layout_below="@id/Alert2"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"/>
</RelativeLayout>
谢谢!!
【问题讨论】:
-
发布您尝试过的代码。
-
@Masum 我在问题中发布了代码。谢谢
-
@SagarNayak 我现在刚刚发布了 XML。抱歉我的回复晚了
标签: android android-studio textview android-button