我有一个XML文件:my_form.xml(仅显示相关视图)。微调器由strings.xml
中的字符串数组填充<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:id="@+id/task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/task_array"
android:prompt="@string/task" />
<TextView android:id="@+id/addTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add another task"
android:onClick="onClick"
android:clickable="true"/>
</LinearLayout>
</ScrollView>
所以我有一个显示xml微调器和文本视图的活动。
单击文本视图时,我想添加相同xml微调器和文本视图的另一个实例,以便一个人可以添加任意数量的任务。
我不想要多选listView,因为我必须将每个选择的项目/任务与其他单独的方法相关联
有人可以帮我吗?
public class MyFormActivity extends FragmentActivity implements
OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_form);
final Spinner spinnerTask = (Spinner) findViewById(R.id.task);
ArrayAdapter<CharSequence> adapter_task = ArrayAdapter
.createFromResource(this, R.array.task_array,
android.R.layout.simple_spinner_item);
spinnerTask.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long row) {
int taskChoice = spinnerTask.getSelectedItemPosition();
SharedPreferences sharedPref = getSharedPreferences("FileName",
0);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("taskChoiceSpinner", taskChoice);
prefEditor.commit();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
View tvAddTask = (TextView) findViewById(R.id.addTask);
tvAddTask.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Here I want add another instance of the spinner and text view above
}
}
答案 0 :(得分:0)
你可以编写一个调用微调器的方法,这个方法将在spinner.setOnItemSelectedListener()中调用;
答案 1 :(得分:0)
试试这个
1 - 将my_form.xml
修改为此
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
2 - 创建新的布局xml文件text_view.xml
并将其放入其中
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add another task"
android:clickable="true"/>
3 - 创建新的布局xml文件spinner_view.xml
并将其放入其中
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/task_array"
android:prompt="@string/task" />
4 - 像这样编辑你的代码
public class MyFormActivity extends FragmentActivity implements OnClickListener {
private LinearLayout linear;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_form);
linear = (LinearLayout) findViewById(R.id.LinearLayout01);
addNewView();
}
private void addNewView(){
Spinner spinnerTask = View.inflate(this, R.layout.spinner_view, null);
TextView tvAddTask = View.inflate(this, R.layout.text_view, null);
linear.addView(spinnerTask);
linear.addView(tvAddTask);
spinnerTask.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long row) {
int taskChoice = pos;
SharedPreferences sharedPref = getSharedPreferences("FileName",
0);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("taskChoiceSpinner", taskChoice);
prefEditor.commit();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
tvAddTask.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Here I want add another instance of the spinner and text view above
addNewView();
}
});
}
}
希望这有助于你。