2017-07-31 24 views
0

我有一個ListView的值,我想通過按下它並打開上下文菜單來使每個值都可編輯。如何將EditText字段添加到在單擊ListView中的項目時出現的上下文菜單中?如果您對上述問題有更好的想法,請隨時提出建議。預先感謝任何回答的人!如何在ContextMenu中創建EditText

回答

0

您不必爲此使用上下文菜單。您可以使用AlertDialog。 看看這個答案。
https://stackoverflow.com/a/45352961/8200290

只需使用編輯視圖創建不同的佈局。

這是你的解決方案。

你將不得不編輯的東西,使其適合你的應用程序!

MainActivity類別

public class MainActivity extends AppCompatActivity { 

    List<String> list = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     list.add("Android"); 
     list.add("iPhone"); 
     list.add("Windows"); 
     list.add("Blackberry"); 
     list.add("Mac"); 
     list.add("Laptop"); 
     list.add("LCD"); 
     list.add("Dell"); 

     ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_view_item, list); 
     ListView listView = (ListView) findViewById(R.id.mobile_list); 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(MainActivity.this, "Clicked: " + list.get(position), Toast.LENGTH_SHORT).show(); 




       AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); 
       View alertView = getLayoutInflater().inflate(R.layout.custom_alert, null); 


       //Set the view 
       alert.setView(alertView); 
       //Show alert 
       final AlertDialog alertDialog = alert.show(); 
       //Can not close the alert by touching outside. 
       alertDialog.setCancelable(false); 
       alertDialog.setCanceledOnTouchOutside(false); 
       alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

       //Set the edit text for the item clicked 
       EditText editText = (EditText) alertView.findViewById(R.id.editText); 
       editText.setText(list.get(position)); 

       ImageView closeButton = (ImageView) alertView.findViewById(R.id.closeButton); 

       closeButton.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         alertDialog.dismiss(); 
        } 
       }); 
      } 
     }); 

     listView.setAdapter(adapter); 
    } 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.vzw.www.listviewalert.MainActivity"> 

    <ListView 
     android:id="@+id/mobile_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

list_view_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dip" 
    android:textSize="16dip" 
    android:textStyle="bold" > 
</TextView> 

custom_alert.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="match_parent" 
    android:padding="20dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/alertContainer" 
     android:background="@drawable/custom_alert_bg"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:id="@+id/rowOne"> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/editText"/> 

     </LinearLayout> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

    </RelativeLayout> 

    <ImageView 
     android:id="@+id/closeButton" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_weight="0.33" 
     android:background="#cdcdcd" /> 

</RelativeLayout> 

@繪製/ custom_alert_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid 
     android:color="#ffffff"/> 
    <corners 
     android:radius="5dp" /> 
    <padding 
     android:left="10dp" 
     android:top="10dp" 
     android:right="10dp" 
     android:bottom="10dp" /> 
</shape> 

你得到如下:

enter image description here

enter image description here

enter image description here

+0

我不想製作一個帶有listview的alertdialog,我想做一個listview,當按下其中一個項目時打開alertdialog。 – Keith

+0

無論我發帖,萌芽。我正在向您展示如何創建自定義AlertDialog。您只需點擊列表中的某個項目即可使用此概念。只需編輯警報的佈局....這裏很簡單的東西。 – DroiDev

相關問題