XMLファイルに相対レイアウトがあり、ボタンが含まれています。今、私はこのボタンを押すと、2つのTextViewを作成します。私はAndroid Studioを初めて使うので、助けてください。私はボタンのonClickListenerを作成しようとしましたが、XMLにある現在の相対レイアウトのオブジェクトを取得するために問題が発生しています。Androidスタジオの相対レイアウトにテキストビューを追加する
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
@Masum私は質問にコードを掲載しました。ありがとう – user2950895
あなたのXMLはどこですか? –