2015-10-29 64 views
2

我是新來開發Android ... 我創建了一個setting.java使用字符串和數組適配器列表視圖:如何創建一個彈出菜單,當我點擊listview(android studio)中的項目時顯示?

public class setting extends Activity { 
ListView listView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.setting_layout); 
    String[] settingOptions = new String[]{getString(R.string.settingInterface), getString(R.string.settingLanguages), getString(R.string.settingInfo)}; 
    ArrayAdapter<String> adapter = new ArrayAdapter<String> 
      (this, android.R.layout.simple_list_item_1, android.R.id.text1, settingOptions); 
    listView = (ListView) findViewById(R.id.listView); 
    listView.setAdapter(adapter); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     } 

    }); 



} 
} 

,這是我setting_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ListView 
    android:id="@+id/listView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentBottom="true" /> 
</RelativeLayout> 

setting_layout Activity(listView)

我想創建語言項目的彈出菜單,當我點擊它時,顯示一些其他語言的翻譯。 我已經創建了其他語言的string.xml,但我不知道如何在彈出菜單中使用它。 1:如何在點擊語言項目時顯示彈出菜單? 2:如何在彈出菜單中添加其他語言? 在此先感謝

回答

0

的彈出您可以使用對話框生成器創建一個對話框,它可以是一個自定義(XML佈局),或者可以動態創建(編程方式使用JAVA) 檢查出這個代碼的彈出鑑於:)

,比如我有這個功能,我在裏面的onClick撥打:

private void showFlavorDialog(){ 

     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 
     View promptView; 
     LayoutInflater layoutInflater = LayoutInflater.from(myActivity.this); //this gets the custom layout inflater 
     promptView = layoutInflater.inflate(R.layout.dialog_flavors_ar, null); // here you put the custom xml leyout name 
     final EditText editText = (EditText) promptView.findViewById(R.id.editText_note); //my layout has an edit text and button in it and I want to use it, so call the findviewbyid 
     Button add_note = (Button) promptView.findViewById(R.id.button_note); 
     editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); //this removes full screen keyboard, so the keyboard doesnt take the whole screen. 
     add_note.setOnClickListener(new View.OnClickListener() { //onClick for the button 
      @Override 
      public void onClick(View v) { 
       otherNote_b = true; 
       flavor = editText.getText().toString(); 
       new httpSetFlavor().execute(); 
       alertDialogFL.cancel(); 
      } 
     }); 
     if (selectedflavorNameEn.size() > 1) { 
      flavorsGrid = (GridView) promptView.findViewById(R.id.gridView_flavors); // I also have a gridview in the layout 
      flavorsGridAdapter adapter = new flavorsGridAdapter(OrderActivityAr.this, 
        selectedflavorNameEn); 
      flavorsGrid.setAdapter(adapter); // this is a custom adapter for the grid which you wont need 
     } 

     alertDialogBuilder.setTitle(R.string.flavor_ar); //title of the popup window 
     alertDialogBuilder.setView(promptView); //set view of the popup to your custom view 
     alertDialogBuilder.setCancelable(true); //this makes the popup cancelable on back button pressed 
     alertDialogBuilder.setPositiveButton(R.string.delete_flavor_ar, new DialogInterface.OnClickListener() { //this is a button for the popup if you want one 
      public void onClick(DialogInterface dialog, int id) { 
       deleteFlavor = true; 
       new httpSetFlavor().execute(); 
      } 
     }); 
     alertDialogBuilder.setNegativeButton(R.string.back_ar, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }); 
     AlertDialog alertDialogFL = alertDialogBuilder.create(); //here you create the dialog using its builder 
     alertDialogFL.show(); // show it on screen 

    } 

,這是我的自定義佈局的XML文件,所以你可以把你在它想要的任何佈局,並使用它稍後:)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <GridView 
     android:layout_width="wrap_content" 
     android:layout_height="150dp" 
     android:id="@+id/gridView_flavors" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:numColumns="3" 
     android:horizontalSpacing="2dp" 
     android:verticalSpacing="2dp" /> 



    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/gridView_flavors" 
     android:layout_marginTop="5dp"> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:paddingRight="10dp" 
      android:paddingLeft="10dp" 
      android:layout_width="wrap_content" 
      android:singleLine="true" 
      android:layout_height="wrap_content" 
      android:text="@string/add_note_ar" 
      android:id="@+id/button_note" 
      android:layout_below="@+id/gridView_flavors" 
      android:textSize="20dp" 
      android:background="@drawable/button_plain_green" 
      android:textColor="#000" 
      android:layout_weight="0" /> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText_note" 
      android:layout_below="@+id/gridView_flavors" 
      android:layout_toRightOf="@+id/other_text" 
      android:layout_marginTop="2dp" 
      android:layout_toLeftOf="@+id/button_note" 
      android:layout_toStartOf="@+id/button_note" 
      android:layout_weight="1" 
      android:gravity="right" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/other_note_ar" 
      android:id="@+id/other_text" 
      android:layout_below="@+id/gridView_flavors" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_alignBottom="@+id/editText_note" 
      android:gravity="center" 
      android:textSize="20dp" 
      android:layout_weight="0" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" /> 

     <com.andexert.library.RippleView 
      android:layout_width="wrap_content" 
      android:id="@+id/ripple_button" 
      android:layout_weight="0" 
      android:layout_height="wrap_content"></com.andexert.library.RippleView> 
    </TableRow> 
</RelativeLayout> 

這是佈局的外觀:

enter image description here

,並在你的情況,你可以使用自定義佈局列表視圖以及:)但是例如自定義列表項的單選按鈕。

用於顯示保存在XML 好,你應該讓他們保存在RES/strings.xml中 你可以在一個字符串數組像這樣使用字符串其他語言:

String strArray[]={ 
getResources().getString(R.string.string1), 
getResources().getString(R.string.string2), 
getResources().getString(R.string.string3), 
getResources().getString(R.string.string4) 

}; 
相關問題