在Android的动态微调添加一个按钮点击 [英] Add spinner with click of a button dynamically in Android

查看:148
本文介绍了在Android的动态微调添加一个按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图微调使用以下code动态添加到我的应用程序:

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    STARTTIME =(EditText上)findViewById(R.id.EditTextST);
    的startDate =(EditText上)findViewById(R.id.editTextSD);    LinearLayout中的LinearLayout =新的LinearLayout(本);
    //的RelativeLayout的LinearLayout =(RelativeLayout的)findViewById(R.id.main_layout);    ArrayList的<串GT; spinnerArray =新的ArrayList<串GT;();
    spinnerArray.add(1);
    spinnerArray.add(二);
    spinnerArray.add(三);
    spinnerArray.add(四大);
    spinnerArray.add(十二五);    微调微调=新的微调(本);
    ArrayAdapter<串GT; spinnerArrayAdapter =新ArrayAdapter<串GT;(这一点,android.R.layout.simple_spinner_dropdown_item,spinnerArray);
    spinner.setAdapter(spinnerArrayAdapter);    linearlayout.addView(微调);    的setContentView(LinearLayout中);
}

这增加了微调,但替换我的XML与动态创建的微调我的一切。
我想通过点击一个按钮,而不是取代布局添加微调器布局(我已使用XML创建)。请帮忙。

编辑:这是我的XML文件

 <?XML版本=1.0编码=UTF-8&GT?;<滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>< RelativeLayout的
    机器人:ID =@ + ID / main_layout
    机器人:layout_width =match_parent
    机器人:layout_height =900dp>    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:文字=@字符串/ select_week
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>    <复选框
        机器人:ID =@ + ID / checkBox1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / textView1
        机器人:文字=@字符串/太阳/>    <复选框
        机器人:ID =@ + ID / checkBox2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / checkBox1
        机器人:layout_alignBottom =@ + ID / checkBox1
        机器人:layout_toRightOf =@ + ID / checkBox1
        机器人:文字=@字符串/月/>    <复选框
        机器人:ID =@ + ID / checkBox3
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBottom =@ + ID / checkBox2
        机器人:layout_toRightOf =@ + ID / checkBox2
        机器人:文字=@字符串/星期二/>    <复选框
        机器人:ID =@ + ID / checkBox4
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / checkBox3
        机器人:layout_alignBottom =@ + ID / checkBox3
        机器人:layout_toRightOf =@ + ID / checkBox3
        机器人:文字=@字符串/结婚/>    <复选框
        机器人:ID =@ + ID / checkBox5
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / checkBox1
        机器人:layout_toLeftOf =@ + ID / checkBox2
        机器人:文字=@字符串/星期四/>    <复选框
        机器人:ID =@ + ID / checkBox6
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / checkBox2
        机器人:layout_below =@ + ID / checkBox2
        机器人:文字=@字符串/星期五/>    <复选框
        机器人:ID =@ + ID / checkBox7
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / checkBox2
        机器人:layout_toRightOf =@ + ID / checkBox2
        机器人:文字=@字符串/ SAT/>    <的TextView
        机器人:ID =@ + ID / textView2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / checkBox5
        机器人:layout_marginTop =18dp
        机器人:文字=@字符串/ select_week_number
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>    <复选框
        机器人:ID =@ + ID / CheckBox01
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / checkBox02
        机器人:layout_alignBottom =@ + ID / checkBox02
        机器人:layout_alignLeft =@ + ID / checkBox5
        机器人:文字=@字符串/一/>    <复选框
        机器人:ID =@ + ID / CheckBox03
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / checkBox02
        机器人:layout_alignBottom =@ + ID / checkBox02
        机器人:layout_toRightOf =@ + ID / checkBox6
        机器人:文字=@字符串/三/>    <复选框
        机器人:ID =@ + ID / checkBox02
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / textView2
        机器人:layout_toRightOf =@ + ID / checkBox5
        机器人:文字=@字符串/两个/>    <复选框
        机器人:ID =@ + ID / CheckBox04
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / CheckBox03
        机器人:layout_alignBottom =@ + ID / CheckBox03
        机器人:layout_toRightOf =@ + ID / textView2
        机器人:文字=@字符串/四/>    <的TextView
        机器人:ID =@ + ID / TextView01
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / CheckBox01
        机器人:layout_marginTop =16DP
        机器人:文字=@字符串/ select_start_date
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>    <的EditText
        机器人:ID =@ + ID / editTextSD
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / CheckBox01
        机器人:layout_below =@ + ID / TextView01
        机器人:layout_toLeftOf =@ + ID / CheckBox04
        机器人:EMS =10
        安卓的inputType =TEXT |日期
        安卓的onClick =showDatePickerDialog>        < requestFocus的/>
    < /&的EditText GT;    <的EditText
        机器人:ID =@ + ID / EditTextST
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / editTextSD
        机器人:layout_alignBottom =@ + ID / editTextSD
        机器人:layout_alignLeft =@ + ID / CheckBox04
        机器人:EMS =10
        安卓的inputType =时间
        安卓的onClick =showTimePickerDialog/>    <的TextView
        机器人:ID =@ + ID / TextView02
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / editTextSD
        机器人:layout_below =@ + ID / editTextSD
        机器人:layout_marginTop =17dp
        机器人:文字=@字符串/ select_end_date
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>    <的EditText
        机器人:ID =@ + ID / EditText01
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / TextView02
        机器人:layout_alignRight =@ + ID / editTextSD
        机器人:layout_below =@ + ID / TextView02
        机器人:EMS =10
        安卓的inputType =TEXT |日期
        安卓的onClick =showDatePickerDialog/>    <的EditText
        机器人:ID =@ + ID / EditText02
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / EditText01
        机器人:layout_alignBottom =@ + ID / EditText01
        机器人:layout_alignLeft =@ + ID / EditTextST
        机器人:EMS =10
        安卓的inputType =时间
        安卓的onClick =showTimePickerDialog/>    <的TextView
        机器人:ID =@ + ID / textView3
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / EditText01
        机器人:layout_below =@ + ID / EditText01
        机器人:layout_marginTop =17dp
        机器人:文字=@字符串/提醒
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>    <微调
        机器人:ID =@ + ID / spinner1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / textView3
        机器人:项=@阵列/ reminder_times
        机器人:paddingLeft = - 10dp
        机器人:重力=左
        工具:列表项=@安卓布局/ activity_list_item/>    <按钮
        机器人:ID =@ + ID /按钮1
        风格=机器人:ATTR / buttonStyleSmall
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / textView3
        机器人:layout_below =@ + ID / spinner1
        机器人:layout_marginTop =119dp
        机器人:文字=@字符串/ add_reminder/>< / RelativeLayout的>< /滚动型>


解决方案

这是你做了什么


  1. 增加了一个新的LinearLayout。

  2. 增加了一个微调,以上面的布局。

  3. 你设置的内容视图。

这是我认为会工作。


  1. 您使用获取根布局 findViewById(R.id.root_layout)

  2. 现在,创建一个spinner.Add项目把它作为你在code上面做正确。

  3. 现在,微调添加到您的 root_layout

您的错误
你犯了一个新的布局,并将其设置为主要内容,你永远不把它添加到root_layout。

调试
如果你想看看有什么布局层次布局has.Open DDMS视图,option.Click单击位于摄像头附近的层次结构浏览器选项there.Its,并得到了屏幕,你会看到你的布局层次,这将显示包含的LinearLayout一个微调。

下面是一个code来证明它。

 包com.example.testproject;

进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.ArrayAdapter;
进口android.widget.LinearLayout;
进口android.widget.RelativeLayout;
进口android.widget.Spinner;

公共类MainActivity延伸活动{

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    / **
     *这是你做了什么!
     * /
    的LinearLayout布局=新的LinearLayout(本);
    串[]字符串= {1,2,3};
    微调微调=新的微调(本);
    spinner.setAdapter(新ArrayAdapter<串GT;(这一点,android.R.layout.simple_spinner_dropdown_item,字符串));    layout.addView(微调);
    / **
     *当您使用以下提到的解决方案评论此行。
     * /
    的setContentView(布局);    / **
     *这是你应该做了什么。
     * /     RelativeLayout的root_layout =(RelativeLayout的)findViewById(R.id.root_layout);
     root_layout.addView(微调);}

}

希望它可以帮助你。

I was trying to add spinner to my app dynamically by using the following code:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startTime = (EditText)findViewById(R.id.EditTextST);  
    startDate = (EditText)findViewById(R.id.editTextSD); 

    LinearLayout linearlayout = new LinearLayout(this);
    //RelativeLayout linearlayout = (RelativeLayout)findViewById(R.id.main_layout);

    ArrayList<String> spinnerArray = new ArrayList<String>();
    spinnerArray.add("one");
    spinnerArray.add("two");
    spinnerArray.add("three");
    spinnerArray.add("four");
    spinnerArray.add("five");

    Spinner spinner = new Spinner(this);
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinnerArray);
    spinner.setAdapter(spinnerArrayAdapter);

    linearlayout.addView(spinner);

    setContentView(linearlayout);
}

This adds the spinner but replaces everything I have in my XML with the dynamically-created spinner. I want to add the spinner to the layout (which I have created by using XML) by clicking a button, not replacing the layout. Please help.

EDIT: Here is my XML file

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="900dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/select_week"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:text="@string/sun" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/checkBox1"
        android:layout_alignBottom="@+id/checkBox1"
        android:layout_toRightOf="@+id/checkBox1"
        android:text="@string/mon" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/checkBox2"
        android:layout_toRightOf="@+id/checkBox2"
        android:text="@string/tue" />

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/checkBox3"
        android:layout_alignBottom="@+id/checkBox3"
        android:layout_toRightOf="@+id/checkBox3"
        android:text="@string/wed" />

    <CheckBox
        android:id="@+id/checkBox5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox1"
        android:layout_toLeftOf="@+id/checkBox2"
        android:text="@string/thu" />

    <CheckBox
        android:id="@+id/checkBox6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox2"
        android:layout_below="@+id/checkBox2"
        android:text="@string/fri" />

    <CheckBox
        android:id="@+id/checkBox7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox2"
        android:layout_toRightOf="@+id/checkBox2"
        android:text="@string/sat" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/checkBox5"
        android:layout_marginTop="18dp"
        android:text="@string/select_week_number"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <CheckBox
        android:id="@+id/CheckBox01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/checkBox02"
        android:layout_alignBottom="@+id/checkBox02"
        android:layout_alignLeft="@+id/checkBox5"
        android:text="@string/one" />

    <CheckBox
        android:id="@+id/CheckBox03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/checkBox02"
        android:layout_alignBottom="@+id/checkBox02"
        android:layout_toRightOf="@+id/checkBox6"
        android:text="@string/three" />

    <CheckBox
        android:id="@+id/checkBox02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_toRightOf="@+id/checkBox5"
        android:text="@string/two" />

    <CheckBox
        android:id="@+id/CheckBox04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/CheckBox03"
        android:layout_alignBottom="@+id/CheckBox03"
        android:layout_toRightOf="@+id/textView2"
        android:text="@string/four" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/CheckBox01"
        android:layout_marginTop="16dp"
        android:text="@string/select_start_date"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editTextSD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/CheckBox01"
        android:layout_below="@+id/TextView01"
        android:layout_toLeftOf="@+id/CheckBox04"
        android:ems="10"
        android:inputType="text|date"
        android:onClick="showDatePickerDialog" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/EditTextST"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editTextSD"
        android:layout_alignBottom="@+id/editTextSD"
        android:layout_alignLeft="@+id/CheckBox04"
        android:ems="10"
        android:inputType="time"
        android:onClick="showTimePickerDialog" />

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextSD"
        android:layout_below="@+id/editTextSD"
        android:layout_marginTop="17dp"
        android:text="@string/select_end_date"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/EditText01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView02"
        android:layout_alignRight="@+id/editTextSD"
        android:layout_below="@+id/TextView02"
        android:ems="10"
        android:inputType="text|date"
        android:onClick="showDatePickerDialog" />

    <EditText
        android:id="@+id/EditText02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/EditText01"
        android:layout_alignBottom="@+id/EditText01"
        android:layout_alignLeft="@+id/EditTextST"
        android:ems="10"
        android:inputType="time"
        android:onClick="showTimePickerDialog" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/EditText01"
        android:layout_below="@+id/EditText01"
        android:layout_marginTop="17dp"
        android:text="@string/reminder"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView3"
        android:entries="@array/reminder_times"
        android:paddingLeft="-10dp"
        android:gravity="left"
        tools:listitem="@android:layout/activity_list_item" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/spinner1"
        android:layout_marginTop="119dp"
        android:text="@string/add_reminder" />



</RelativeLayout>

</ScrollView>

解决方案

This is what You have done

  1. Added a new LinearLayout.
  2. Added a spinner to the above layout.
  3. And you set the content view.

This is What i think will work.

  1. You get the root layout using findViewById(R.id.root_layout).
  2. Now create a spinner.Add items to it as you did correctly above in your code.
  3. Now add the spinner to your root_layout.

Your Mistake You made a new layout and set it as the main content and you never added it to the root_layout.

Debugging If you want to see what layout hierarchy your layout has.Open ddms view and click on the heirarchy Viewer option there.Its located near the camera option.Click it and get the screen and you will see your layout hierarchy which will display a linearLayout containing a spinner.

Here's a code to demonstrate it.

package com.example.testproject;

import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Spinner;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /**
     * This is what you have done!!!
     */
    LinearLayout layout=new LinearLayout(this);
    String[] strings={"1","2","3"};
    Spinner spinner=new Spinner(this);
    spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,strings));

    layout.addView(spinner);
    /**
     * Comment this line when you use the below mentioned solution.
     */
    setContentView(layout);

    /**
     * This is what you should have done.
     */

     RelativeLayout root_layout=(RelativeLayout)findViewById(R.id.root_layout);
     root_layout.addView(spinner);

}

}

Hope it helps you.

这篇关于在Android的动态微调添加一个按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆