您好我最近来到了android技术,在我的应用程序中我有两个按钮(一个用于显示电影列表,另一个用于显示countrylist)
当我点击第二个按钮时,我想在旋转列表中显示电影列表,如下图所示。
但是根据我的代码,当我点击按钮时,第一个微调器正在出现,我选择任何一个项目并将其设置为我的按钮标题但是在选择后微调器仍然可见B / w两个按钮就像我的第二个屏幕一样去掉它。
如何解决此问题?
请帮帮我。
我想点击按钮时直接显示微调列表。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Button1Action"
android:text="CountiesList"/>
<Spinner
android:visibility="gone"
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mainLayout"
android:layout_alignParentRight="true"
android:paddingRight="0dp"
android:layout_marginTop="5dp"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Button2Action"
android:text="MoviesList"/>
<Spinner
android:visibility="gone"
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mainLayout"
android:layout_alignParentRight="true"
android:paddingRight="0dp"
android:layout_marginTop="5dp"
/>
</LinearLayout>
public class spinnerListProgramatically extends AppCompatActivity{
String [] countriesList = {"india","usa","england"
};
String [] moviesList = {"fury","300 rise of an empire","troy"
};
Spinner spinner1,spinner2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerlist_runtime);
}
public void Button1Action(View view){
spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
spinner1.setVisibility(View.VISIBLE);
}
public void Button2Action(View view){
spinner2 = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, moviesList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
spinner2.setVisibility(View.VISIBLE);
}
}
答案 0 :(得分:5)
为什么不删除Spinner
而只改为Button
。拥有Spinner
和Button
两者都没有意义。
根据您展示的图片,您需要PopupMenu
:
public void Button2Action(View view){
showFilterPopup(view);
}
private void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
// Inflate the menu from xml
popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());
// Setup menu item selection
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.troy:
Toast.makeText(MainActivity.this, "troy", Toast.LENGTH_SHORT).show();
return true;
case R.id.rise:
Toast.makeText(MainActivity.this, "300 Rise of Empire", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
});
// Handle dismissal with: popup.setOnDismissListener(...);
// Show the menu
popup.show();
}
您的R.menu.popup
将包含您需要的所有项目。
当您拥有静态数据集时,此方法非常有用。
只需几步就可以让它变得动态。希望这会有所帮助。
答案 1 :(得分:2)
好的,经过长时间的讨论,我终于明白了你的意思。我已经为您提供了完整的源代码,这些代码经过优化和修改,可以从您的代码中进
<强> XML 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_marginTop="50dp"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Button1Action"
android:text="CountiesList"/>
<Spinner
android:visibility="invisible"
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
/>
<Spinner
android:visibility="gone"
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Button2Action"
android:text="MoviesList"/>
</LinearLayout>
您的活动
public class spinnerListProgramatically extends AppCompatActivity {
Button button1, button2;
String [] countriesList = {"NONE","india","usa","england"};
String [] moviesList = {"NONE","fury","300 rise of an empire","troy" };
Spinner spinner1,spinner2;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Your other setup codes
spinner1 = (Spinner)findViewById(R.id.spinner1);
spinner2 = (Spinner)findViewById(R.id.spinner2);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
}
public void Button1Action(View view){
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
spinner1.setVisibility(View.VISIBLE);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position!=0) {
button1.setText(countriesList[position]);
spinner1.setVisibility(View.GONE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void Button2Action(View view){
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, moviesList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
spinner2.setVisibility(View.VISIBLE);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position!=0) {
button2.setText(moviesList[position]);
spinner2.setVisibility(View.GONE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
答案 2 :(得分:1)
使用它可以正常工作
public void Button1Action(View view){
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
spinner1.setVisibility(View.INVISIBLE);
spinner1.performClick();
}
public void Button2Action(View view){
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, moviesList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
spinner2.setVisibility(View.INVISIBLE);
spinner2.performClick();
}
查看此屏幕
可以将其设置为屏幕显示。
答案 3 :(得分:0)
无法评论因为我没有那么多的声誉..你可以更具体地说明你想要实现的目标吗?
问题在于
if(spinner2.getSelectedItem() == "fury") {
// for checking user havent selected anything
spinner2.performClick();
}
if(spinner1.getSelectedItem() == "india") {
// for checking user havent selected anything
spinner1.performClick();
}
不为空..默认情况下,它会执行第一个值
所以请改用此代码
public void Button1Action(View view){
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesList);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
spinner1.setVisibility(View.VISIBLE);
String s = (String) spinner1.getSelectedItem();
spinner1.performClick();
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
button.setText(""+ spinner1.getSelectedItem());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
并设置按钮的标题
button = (Button) findViewById(R.id.button1);
button1 = (Button) findViewById(R.id.button2);
按钮onCLick 并初始化onCreate
上的按钮void parseOGTag(String response) {
// Parse og tags
Document doc = Jsoup.parse(response);
Elements ogTags = doc.select("meta[property^=og:]");
if (ogTags.size() <= 0) {
return;
}
// Set OGTags you want
String title;
String desc;
String image;
for (int i = 0; i < ogTags.size(); i++) {
Element tag = ogTags.get(i);
String text = tag.attr("property");
if ("og:image".equals(text)) {
image = tag.attr("content");
} else if ("og:description".equals(text)) {
desc = tag.attr("content");
} else if ("og:title".equals(text)) {
title = tag.attr("content");
}
}
}
并且放置适配器不是问题..
答案 4 :(得分:0)
在不轻按微调器的情况下显示微调器列表项
Spinner spi_type = findViewById(R.id.spi_type);
Button button = findViewById(R.id.button);
imgVIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// performClick() this method show Spinner-List item without tap on spinner
spi_type.performClick();
}
});
答案 5 :(得分:-1)