2016-05-09 31 views
0

我寫了一個應用程序,它必須調用對話框來獲取名稱。我需要對話框中的自定義視圖,所以我使用.setContentView()。但是在api少於20的情況下,我在顯示它時遇到了問題。 這是調用對話框:我應該用什麼來代替android api中的自定義對話框20

public void setName(View v) { 
     nameDialog = new Dialog(this); 
     //using our dialog 
     nameDialog.setContentView(R.layout.new_name); 
     EditText text = (EditText) nameDialog.findViewById(R.id.form2_dialog_name); 
     //if we have previous name we show it 
     if (haveName) { 
      text.setText(((Button) findViewById(R.id.form2_name_button)).getText()); 
     } 
     //request focus and call keyboard for input name 
     text.requestFocus(); 
     text.selectAll(); 
     nameDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
     nameDialog.show(); 
    } 

xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@android:color/holo_blue_dark" 
    tools:context=".CreateNoteActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="@string/form2_dialog_name_of_event" 
     android:id="@+id/textView11" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:text="" 
     android:ems="10" 
     android:id="@+id/form2_dialog_name" 
     android:maxLines="1" /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end"> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/close_button" 
      android:id="@+id/button8" 
      android:onClick="onCancelDialog"/> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/okay_button" 
      android:id="@+id/okay_form2_dialog_button" 
      android:onClick="onFinishDialog" /> 
    </LinearLayout> 

</LinearLayout> 

如何它看起來在API上,然後20: http://i.imgur.com/pJ1oZky.png

小於20: http://i.imgur.com/TJGEwXh.png

+0

您的圖片相同=( –

+0

我固定它,謝謝:) –

+0

確定不同的鏈接,但仍然沒有差xd –

回答

0

OK答案很簡單:使用android.support.v7.app.AlertDialog

你的setName將不得不尋找這樣便:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    LayoutInflater inflater = this.getLayoutInflater(); 
    View dialogView = inflater.inflate(R.layout.new_name, null); 
    builder.setView(dialogView); 

    EditText text = (EditText) dialogView.findViewById(R.id.form2_dialog_name); 
    //if we have previous name we show it 
    if (haveName) { 
     text.setText(((Button) findViewById(R.id.form2_name_button)).getText()); 
    } 
    //request focus and call keyboard for input name 
    text.requestFocus(); 
    text.selectAll(); 

    AlertDialog dialog = builder.create(); 
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
    dialog.show(); 

我想你將不得不用一下你的佈局文件,因爲按鈕都相當有點遠,但我認爲這將是隻是一個小問題;)

+1

「謝謝你的上帝!謝謝!」 - 那是我年輕時在一個英語場景中的言語。但現在我想說完全一樣。 (因爲從那時起我的英語沒有得到改善:)) –

相關問題