2016-05-03 35 views
-1

我在我的XML文件中有一個相對佈局,它包含一個按鈕。現在我希望當我按下這個按鈕時,它會創建2個TextViews。任何幫助,因爲我是Android Studio新手?我已經嘗試創建按鈕的onClickListener,但我有問題才能獲取我在XML中的當前相對佈局的對象。將文本視圖添加到Android Studio中的相對佈局

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
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'關鍵字上看到'佈局'這個詞出現錯誤。無論如何,你知道我做錯了什麼嗎?謝謝!! – user2950895

相關問題