Spinner怎么在Dialog中使用

Spinner怎么在Dialog中使用?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)建站成都網(wǎng)站建設(shè)按需網(wǎng)站策劃,是成都網(wǎng)站設(shè)計公司,為資質(zhì)代辦提供網(wǎng)站建設(shè)服務(wù),有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設(shè)計服務(wù):原型圖制作、網(wǎng)站創(chuàng)意設(shè)計、前端HTML5制作、后臺程序開發(fā)等。成都網(wǎng)站改版熱線:18982081108

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初始化,加載,顯示出來的完整代碼(包含對Spinner進行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,感覺效果也不是特別讓人滿意。

截張圖:

 Spinner怎么在Dialog中使用

在Github上找到一個不錯的項目,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)的。

Spinner怎么在Dialog中使用

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

網(wǎng)頁標(biāo)題:Spinner怎么在Dialog中使用
文章路徑:http://muchs.cn/article26/ipghcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、虛擬主機、外貿(mào)建站手機網(wǎng)站建設(shè)、網(wǎng)站設(shè)計公司、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)