gpt4 book ai didi

android - 如何使微调器工作?

转载 作者:行者123 更新时间:2023-11-29 15:09:11 25 4
gpt4 key购买 nike

enter image description here大家好,我是微调器的新手,出于某种原因,当我单击微调器时,无法获得下拉菜单。我是否需要在 onItemSelected 方法中添加一些内容才能使其下拉?

这里是一些基本的代码。

public class MyActivity extends Activity implements View.OnClickListener
{
private Spinner dropDownspinner;
private ArrayAdapter menuAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
//spinner
dropDownspinner = (Spinner) findViewById(R.id.spinner);

menuAdapter.createFromResource(this, R.array.Drop_Down_Menu, android.R.layout.simple_spinner_item);

dropDownspinner.setAdapter(menuAdapter);

dropDownspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
}
}

字符串数组:

<!--Main Spinner-->
<string name="Profile">Profile</string>
<string name="Friends">Friends </string>
<string name="Folders">Folders</string>
<string name="Edit">Setting</string>
<string-array name="Drop_Down_Menu">
<item></item>
</string-array>

在我的布局中。

    <Spinner
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/spinner"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/textView6"
android:layout_toStartOf="@+id/textView6" />

最佳答案

Do I need to have something inside the onItemSelected method for it to drop down?

不,但是您必须在 SpinnerAdapter 上调用 setDropDownViewResource() 来说明下拉条目应该是什么样子:

/***
Copyright (c) 2008-2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.

From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/

package com.commonsware.android.selection;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class SpinnerDemo extends Activity
implements AdapterView.OnItemSelectedListener {
private TextView selection;
private static final String[] items={"lorem", "ipsum", "dolor",
"sit", "amet",
"consectetuer", "adipiscing", "elit", "morbi", "vel",
"ligula", "vitae", "arcu", "aliquet", "mollis",
"etiam", "vel", "erat", "placerat", "ante",
"porttitor", "sodales", "pellentesque", "augue", "purus"};

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection=(TextView)findViewById(R.id.selection);

Spinner spin=(Spinner)findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);

ArrayAdapter<String> aa=new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
items);

aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}

@Override
public void onItemSelected(AdapterView<?> parent,
View v, int position, long id) {
selection.setText(items[position]);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}
}

关于android - 如何使微调器工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33684249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com