欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spinner在Dialog中的使用效果實(shí)例代碼詳解

 更新時(shí)間:2019年05月14日 11:05:35   作者:xing_star  
這篇文章主要介紹了Spinner在Dialog中的使用效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

背景:

記得很久以前,碰到一個(gè)需求場(chǎng)景,需要在Android Dialog中顯示Spinner,用來進(jìn)行選擇操作。那個(gè)時(shí)候還很困惑,不知道是否可以這么搞。抱著試試看的心態(tài),做起了實(shí)驗(yàn),看起來效果還可行,不過最終還是選用了一個(gè)開源項(xiàng)目,效果看起來更棒。

代碼演示:

Spinner在Dialog中的使用,Dialog中關(guān)于view的xml布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:padding="16dp">
 <Spinner
  android:id="@+id/spinner"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginBottom="2dp" />
 <EditText
  android:id="@+id/edit"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:padding="8dp" />
</LinearLayout>

dialog初始化,加載,顯示出來的完整代碼(包含對(duì)Spinner進(jìn)行Adapter設(shè)置)。

private void showAlertDialog() {
 View view = LayoutInflater.from(this).inflate(R.layout.dialog_add_notebook, null);
 Spinner spinner = view.findViewById(R.id.spinner);
 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, R.layout.simple_spinner_item, android.R.id.text1, categories);
 spinner.setAdapter(arrayAdapter);
 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
   Toast.makeText(MainActivity.this, "選中的分類是: " + categories.get(position), Toast.LENGTH_LONG).show();
  }
 
  @Override
  public void onNothingSelected(AdapterView<?> parent) {
 
  }
 });
 new AlertDialog.Builder(this)
   .setTitle("提示")
   .setView(view)
   .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
     dialog.dismiss();
    }
   })
   .show();
}

只能說spinner在dialog中,顯示出來的效果一般般,即使通過自定義item布局,調(diào)整padding,感覺效果也不是特別讓人滿意。

截張圖:

 

在Github上找到一個(gè)不錯(cuò)的項(xiàng)目,https://github.com/Lesilva/BetterSpinner。

修改代碼,替換為BetterSpinner。

在app/build.gradle中添加

compile ‘com.weiwangcn.betterspinner:library:1.1.0'

xml布局文件修改為:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:padding="16dp">
 <com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
  android:id="@+id/spinner"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginBottom="@dimen/activity_vertical_margin"
  android:hint="@string/notebook_choose_notebook_hint" />
 <EditText
  android:id="@+id/edit"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:padding="8dp" />
</LinearLayout>

顯示dialog的方法調(diào)整為

public void onClickedAddNotebook(final String parentNotebookId, List<Notebook> notebooks) {
 View view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_add_notebook, null);
 final EditText mEdit = (EditText) view.findViewById(R.id.edit);
 final MaterialBetterSpinner spinner = (MaterialBetterSpinner) view.findViewById(R.id.spinner);
 final List<Notebook> tempNotebooks = new ArrayList<>();
 tempNotebooks.clear();
 tempNotebooks.addAll(notebooks);
 Notebook rootNoteBook = new Notebook();
 rootNoteBook.setTitle(mActivity.getString(R.string.notebook_default_root_notebook_title));
 tempNotebooks.add(0, rootNoteBook);
 SpinnerArrayAdapter<Notebook> adapter = new SpinnerArrayAdapter<Notebook>(view.getContext(), tempNotebooks) {
  @Override
  public String itemToString(Notebook item) {
   return item.getTitle();
  }
 };
 spinner.setAdapter(adapter);
 spinner.setText(rootNoteBook.getTitle());
 new AlertDialog.Builder(mActivity)
   .setTitle(R.string.add_notebook)
   .setView(view)
   .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
     dialog.dismiss();
     addNotebook(mEdit.getText().toString(), getNotebookId(tempNotebooks, spinner.getText().toString()));
    }
   })
   .show();
}

細(xì)微之處的api有所變化,用法大多差不多,看一下最終的預(yù)覽效果,覺得還是挺materialDesign風(fēng)的。

總結(jié)

以上所述是小編給大家介紹的Spinner在Dialog中的使用效果實(shí)例代碼詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論