android |从菜单按钮创建一个对话框微调器

时间:2015-10-01 10:35:13

标签: android android-spinner android-menu

我想为我的应用创建语言选择器。我在菜单布局中创建了一个按钮,我希望在单击某个选项菜单时打开微调器。我是初学者,所以如果你能解释你的答案,我会很高兴。

2 个答案:

答案 0 :(得分:0)

查看有关创建自定义对话框的这篇文章: http://android-developers.blogspot.co.uk/2012/05/using-dialogfragments.html

恕我直言的纺纱厂不是很灵活。如果我是你,我会在对话框中使用列表视图,但这个选择是你的:)

答案 1 :(得分:0)

首先,您必须创建一个xml布局,其中将放置您的微调器元素

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:padding="10dip"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">



<!-- Spinner Element -->
<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:prompt="Select Language"
/>

</LinearLayout>

然后我在你的活动中你想要显示你应该实现OnItemSelectedListener接口的snipper,以处理微调器的选择

public class SnipperActivity extends Activity implements OnItemSelectedListener{

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //here you get the reference to the spinner element declared in your xml layout
    Spinner spinner = (Spinner) findViewById(R.id.spinner);


  //set the listener to the spinner
    spinner.setOnItemSelectedListener(this);

 //here you create an arraylist for the items to be displayed in your spinner element
  List<String> languages = new ArrayList<String>();
    languages.add("English");
    languages.add("Greek");
  }

//define an adapter for the spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, languages);


 //set the style of the snipper, in this case a listview with a radio button               

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_it em);

 //attach the adapter to your spinner element
  spinner.setAdapter(dataAdapter);

}

要处理微调元素选择,你必须在SnipperActivity类中删除以下方法

 @Override
public void onItemSelected(AdapterView parent, View view, int position, long id) {
    // On selecting a spinner item
    String language = parent.getItemAtPosition(position).toString();

  //show a spinner item
  Log.e("TAG", "Spinner item selected " + language);


}