2016-05-03 12 views
-1

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> 

感謝!!

+0

あなたが試みたあなたのコードを投稿してください。 – Masum

+0

@Masum私は質問にコードを掲載しました。ありがとう – user2950895

+0

あなたのXMLはどこですか? –

答えて

0

このリンクはあなたのために有用である可能性があります displaying a string on the textview when clicking a button in android

それは1つの文字列のために、あなたは下の方法にあなたの2番目のテキストビューを追加することができますされています

private void printmyname(){ 
    System.out.println("coming"); 
} 
0
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_brush_your_teeth); 

    final Button addAlertButton = (Button) findViewById(R.id.AddAlert); 

    addAlertButton.setOnClickListener(new Button.OnClickListener() { 
     public void onClick(View v) { 

      RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout); 

      int prevTextViewId = 0; 
      final TextView textView1 = new TextView(this); 
      final TextView textView2 = new TextView(this); 
      textView1.setText("Text 1"); 
      textView2.setText("Text 2"); 

      int curTextViewId = v.getId(); 

      textView1.setId(curTextViewId+1); 
      textView2.setId(curTextViewId+2); 

      final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

      params.addRule(RelativeLayout.BELOW, v.getId()+1); 
      textView1.setLayoutParams(params); 
      params.addRule(RelativeLayout.BELOW, v.getId()+2); 
      textView2.setLayoutParams(params); 

      layout.addView(textView1, params); 
      layout.addView(textView2, params); 

     } 
    }); 
} 

わからないけど、何かのようにこれはあなたに役立ちます

+0

あなたのコードを私のプロジェクトに貼り付けてコピーします。しかし、私は 'R.id.layout'と 'this'キーワードから 'layout'という単語に誤りがあります。私は間違って何をしているのか分かりますか?ありがとう!! – user2950895

関連する問題