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

android | create a dialog spinner from menu button pressed

我想为我的应用程序创建一个语言选择器。我在菜单布局中创建了一个按钮,我希望在单击其中一个选项菜单时打开一个微调器。我是初学者,如果您能解释一下您的答案,我将不胜感激。

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

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

首先,您必须创建一个 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>

然后我 activity 你想在其中显示 snipper 你应该实现 OnItemSelectedListener 接口来处理微调器的选择

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 中验证以下方法 class

 @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);


}