【问题标题】:Adding a Textview to a Relative Layout in Android Studio在 Android Studio 中将 Textview 添加到相对布局
【发布时间】: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


【解决方案1】:

此链接可能对您有用: displaying a string on the textview when clicking a button in android

这是一个字符串,您可以将第二个文本视图添加到下面的方法中:

private void printmyname(){
    System.out.println("coming");
}

【讨论】:

    【解决方案2】:
    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);
    
            }
        });
    }
    

    不确定,但这样的东西可以帮助你

    【讨论】:

    • 我将您的代码复制粘贴到我的项目中。但是,我在“R.id.layout”和“this”关键字中的“layout”一词上出现错误。无论如何,你知道我做错了什么吗?谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 2022-01-17
    • 2021-04-12
    • 2015-02-04
    • 2012-05-31
    • 2015-10-11
    相关资源
    最近更新 更多