2015-02-24 68 views
-1

我怎樣才能讓我的提醒對話android.please自定義提醒幫助我在自定義提醒中顯示我的對話框。我的代碼粘貼在這裏。該代碼包含10列表視圖的輸出。當點擊列表中的項目時,它可能會收到警告..我怎樣才能讓我的提醒對話在Android的自定義提醒

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Find the ListView resource. 
    mainListView = (ListView) findViewById(R.id.mainListView); 
    // Create and populate a List of planet names. 
    final String[] planets = new String[] { "Allu", "Abin", "Bibin", "Aswathy", 
             "Jibin", "Saran", "Jobin", "Neethu","ammu","Ram"}; 
    final ArrayList<String> planetList = new ArrayList<String>(); 
    planetList.addAll(Arrays.asList(planets)); 

    // Create ArrayAdapter using the planet list. 
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList); 

    /*// Add more planets. If you passed a String[] instead of a List<String> 
    // into the ArrayAdapter constructor, you must not add more items. 
    // Otherwise an exception will occur. 
    listAdapter.add("Ceres"); 
    listAdapter.add("Pluto"); 
    listAdapter.add("Haumea"); 
    listAdapter.add("Makemake"); 
    listAdapter.add("Eris");*/ 

    // Set the ArrayAdapter as the ListView's adapter. 
    mainListView.setAdapter(listAdapter); 
    mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 


     public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 

      AlertDialog.Builder alert = new AlertDialog.Builder(context); 
      alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title here 
      alert.setMessage("Edit Your Name Here"); //Message here 

      final EditText input = new EditText(context); 
      input.setText((String)planetList.get(position)); 
      alert.setView(input); 

      alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        String srt = input.getEditableText().toString(); 

        Toast.makeText(context,srt, Toast.LENGTH_LONG).show(); 
        planetList.set(position, srt); 
        listAdapter.notifyDataSetChanged(); 

       } 
      }); 
      alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        dialog.cancel(); 
       } 
      }); //End of alert.setNegativeButton 
      AlertDialog alertDialog = alert.create(); 
      alertDialog.show(); 
     } 
    }); 

    } 
} 
+0

你想爲你的對話框設計一個XML佈局嗎? – Bimbow 2015-02-24 11:29:27

+0

我想在自定義提醒格式中看到我的提醒 – Anoop 2015-02-24 11:35:44

+0

我需要爲自定義提醒創建另一個xml文件嗎 – Anoop 2015-02-24 11:50:33

回答

0

public class MyDialog extends DialogFragment {

public static MyDialog getInstance() { 
    MyDialog dialog = new MyDialog(); 
    Bundle bundle = new Bundle(); 

    bundle.putInt("your key", "save something"); 

    dialog.setArguments(bundle); 

    return dialog; 
} 


@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    AlertDialog.Builder vBuilder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater = LayoutInflater.from(getActivity()); 
    View view = inflater.inflate(R.layout."your custom layout", null); 

    yourEditText = (TextView) view.findViewById(R.id."your edit text id"); 

    Bundle bundle = getArguments(); 

    if(bundle!=null){ 

     ... do something 
    } 


    vBuilder.setView(view); 

    return vBuilder.create(); 
} 

}

,而不是打開此對話框

MyDialog.getInstance().show(getFragmentManager(), "your TAG"); 
+0

請幫我用上面給出的代碼.. – Anoop 2015-02-24 11:45:09

0

爲什麼你不能使用對話框?

 dialog = new Dialog(this); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.dialog);//create an xml and use it. 
     dialog.setCancelable(true); 
     passwordEmail = (EditText) dialog 
       .findViewById(R.id.dialog_txt_name); 
         dialog.show(); 
+0

其不清楚?? – Anoop 2015-02-24 11:33:24

0

這個:

final EditText input = new EditText(context); input.setText((String)planetList.get(position)); alert.setView(input);

這樣寫:
LayoutInflater inflater = LayoutInflater.from(getActivity()); View view = inflater.inflate(R.layout."your custom layout", null); alert.setView(view);

1

這是一個簡單的方法,使一個自定義XML佈局一個彈出式對話框,它肯定會工作:

讓你的對話框的XML文件。設置android:layout_width="wrap_content"android:layout_height="wrap_content"或任何其他大小。您也可以爲您的佈局設置背景。

例如,這是一個彈出式窗口的XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/popup_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:background="@color/black" > 

    <TextView 
     android:id="@+id/title_tv" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="15dp" 
     android:paddingBottom="5dp" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:text="title" 
     android:textColor="@color/white" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

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

     <TextView 
      android:id="@+id/details_tv" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:paddingTop="40dp" 
      android:paddingBottom="20dp" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:text="Enter your name here:" 
      android:textColor="@color/white" 
      android:layout_weight="1" 
      android:layout_gravity="center"/> 

     <EditText 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:background="@color/white" 
      android:layout_gravity="center" 
      android:layout_marginRight="8dp" 
      android:inputType="textPersonName" 
      android:text="Name" 
      android:ems="10" 
      android:id="@+id/editText" 
      android:layout_weight="1" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:padding="5dp" 
     style="?android:attr/buttonBarStyle" > 

     <Button 
      android:id="@+id/cancel_btn" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Cancel" 
      android:onClick="cancel" 
      android:textColor="@color/white" 
      style="?android:attr/buttonStyle" /> 

     <Button 
      android:id="@+id/exit_btn" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="OK" 
      android:onClick="ok" 
      android:textColor="@color/white" 
      style="?android:attr/buttonStyle" /> 

    </LinearLayout> 

</LinearLayout> 

這就是你如何顯示彈出:

private void showPopup(final Activity context) { 
     LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup_layout); 
     LayoutInflater layoutInflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layoutPopup = layoutInflater.inflate(R.layout.layout_popup, viewGroup); 

     popup = new PopupWindow(context); 
     popup.setContentView(layoutPopup); 
     popup.setWidth(LayoutParams.WRAP_CONTENT); 
     popup.setHeight(LayoutParams.WRAP_CONTENT); 
     popup.setFocusable(true); 

     popup.showAtLocation(layoutPopup, Gravity.CENTER, 0, 0); 
} 

您可以使用簡單的onClick方法按鈕在你彈出的對話框:

public void cancel(View v){ 

    Toast.makeText(getApplicationContext(), "Canceled", 
      Toast.LENGTH_LONG).show(); 

    popup.dismiss(); 
} 

public void ok(View v){ 

    Toast.makeText(getApplicationContext(), "Done", 
      Toast.LENGTH_LONG).show(); 

    popup.dismiss(); 
} 

您還可以閱讀this f或另一種創建自定義對話框的方式。

+0

謝謝你看起來很簡單 – Anoop 2015-02-25 03:22:49

+0

@Anoop請選擇接受的答案,如果它是你在找什麼。 – Bimbow 2015-02-25 06:58:22