如何在Android Studio中通过长按EditText打开弹出窗口

5
当用户长按EditText时,如何打开一个弹窗窗口,让用户输入或粘贴内容,然后点击“确定”按钮将输入的文本放入EditText中,并且我不希望用户使用回车键。
我的主要目的是让用户一次性看到整个文本,因为在我的apk中EditText的尺寸很小。
注意:我是Android编程的初学者,这是我的第一个问题。 :)
4个回答

3
欢迎来到SO。在SO提问之前,您需要进行一些研究。请提供相关信息,这将帮助我们找到答案。
作为回答您的问题,您可以使用setOnclickListener将onclick监听器附加到EditText,以便在用户单击EditText时触发事件。
对于包含EditText的弹出窗口,您可以使用具有自己xml布局的对话框。
请查看此页面以创建和使用对话框。

3

我理解你的问题是:

  • 你想在长按编辑框时打开一个弹出窗口。
  • 将编辑框中的文本复制到弹出窗口中的编辑框中。
  • 在按下“确定”按钮后,将弹出窗口编辑框中的文本复制回基本文本框中。

以下是参考代码:

弹出窗口 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:background="#FFFFE0"
    android:orientation="vertical" 
    android:paddingLeft="7dip" 
    android:paddingTop="7dip"
    android:paddingRight="7dip"
    android:paddingBottom="7dip">

    <EditText
       android:id="@+id/edit_pop"
       android:layout_width="match_parent"
       android:layout_height="120dp"
       android:ems="10"/>

   <Button
      android:id="@+id/btok"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="OK" />

</LinearLayout>

Main activity -

public class MainActivity extends Activity implements OnLongClickListener
{
  EditText vedt=null,edPop;
  Button btOk=null;
  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    vedt = (EditText) findViewById(R.id.editText1);
    vedt.setOnLongClickListener(this); 
}

@Override
public boolean onLongClick(View v) 
{

    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.popup, null);
    final PopupWindow popupWindow = new PopupWindow(popupView,android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    edPop = (EditText)popupView.findViewById(R.id.edit_pop);
    btOk  = (Button)popupView.findViewById(R.id.btok);
    edPop.requestFocus();
    edPop.setText(vedt.getText().toString());
    popupWindow.showAtLocation(popupView, Gravity.CENTER,5,5);
    popupWindow.setOutsideTouchable(false);
    popupWindow.setFocusable(true);
    popupWindow.update();
    btOk.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            vedt.setText(edPop.getText().toString());
            popupWindow.dismiss();
        }
    });

    return false;

}

}


谢谢您的反馈,它对我有用并解决了我的问题。 :) - M10

1
大部分的内容都可以在安卓API页面上找到,正如开发者所说。但是,这个例子是从文档中稍微编辑过的,以帮助您入门。

dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="Enter Text Here" />
</LinearLayout>

把这段代码放在你的activity.java文件中。
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog, null))
    // Add action buttons
           .setPositiveButton("Submit", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // Do something
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });      
    return builder.create();
}

PS. 我没有编译此代码,因此可能会出现错误,但这是概念。


0
Write inside your activity:  

@Override
    public void handleOnLongClick(View v, int elementActionCode) {
        if (ORDER_SUMMARY_POPUP == elementActionCode) {
            View orderSummaryLayout = _initiatePopupWindow();
            if(orderSummaryLayout != null)
                _setOrderSummaryValues(orderSummaryLayout);
        }

private View _initiatePopupWindow() {
        int xCoOrdinate = 25;
        int yCoOrdinate = 25;
        int xWindowPerc = 10;
        int yWindowPerc = 50;
        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();  
        int height = display.getHeight();
        View layout = null;

        xCoOrdinate = ((width*xWindowPerc)/100);
        yCoOrdinate = ((height*yWindowPerc)/100);
        width = width - xCoOrdinate;
        height = height - yCoOrdinate;

        try {
            // We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.order_summary_popup,
                    (ViewGroup) findViewById(R.id.orderPopupWindow));
            popupWindow = new PopupWindow(layout, width, height,
                    true);
            popupWindow.showAtLocation(layout, Gravity.AXIS_X_SHIFT,
                    (xCoOrdinate/2), (yCoOrdinate/2));

        } catch (Exception ex) {
            Log.e(TAG, "Error while initiation of the Order Summary Popup."  +    ex.getMessage());
        }
        return layout;
    }


private void _setOrderSummaryValues(View layout) {
        try {

            ImageView btnClosePopup = (ImageView) layout.findViewById(R.id.btn_close_popup1);
            btnClosePopup.setOnClickListener(new UUIHandlers.UListener(this,ORDER_SUMMARY_POPUP_CLOSE));

            TextView totGrossAmt = (TextView) layout.findViewById(R.id.totalGrossAmt1);
            totGrossAmt.setText(String.valueOf(df.format(totalGrossAmount)));
            totGrossAmt.setText(CommonUtils.getSystemCurrency(this, totGrossAmt, true));

            TextView totDiscAmt = (TextView) layout.findViewById(R.id.totalDiscAmt1);
            totDiscAmt.setText(String.valueOf(df.format(totalDiscAmount)));
            totDiscAmt.setText(CommonUtils.getSystemCurrency(this, totDiscAmt, true));

            TextView totTaxableAmt = (TextView) layout.findViewById(R.id.totalTaxableAmt1);
            totTaxableAmt.setText(String.valueOf(df.format(totalTaxableAmount)));
            totTaxableAmt.setText(CommonUtils.getSystemCurrency(this, totTaxableAmt, true));

            TextView totTaxAmt = (TextView) layout.findViewById(R.id.totalTaxAmt1);
            totTaxAmt.setText(String.valueOf(df.format(totalTaxAmount)));
            totTaxAmt.setText(CommonUtils.getSystemCurrency(this, totTaxAmt, true));

            TextView totBilAmt = (TextView) layout.findViewById(R.id.totalOrderAmount);
            totBilAmt.setText(String.valueOf(df.format(totalOrderAmount)));
            totBilAmt.setText(CommonUtils.getSystemCurrency(this, totBilAmt, true));



            TableLayout orderSummaryTable = (TableLayout) layout
                    .findViewById(R.id.orderSummaryPopupTable);


        } catch (Exception e) {
            Log.e(TAG, "Error while setting Bill Summary Popup values. " + e.getMessage());
        }
    }



Pop up xml file:     

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/orderPopupWindow"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="3dp"
    android:background="@drawable/popup_window_background"
    android:gravity="top"
    android:orientation="vertical"
    android:weightSum="100" >

    <TableLayout
        android:id="@+id/orderSummaryTable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:gravity="top"
        android:orientation="vertical" >

        <!-- Order Summary Header -->

        <TableRow
            android:id="@+id/discAmtRow"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="5dp" >

            <TextView
                android:id="@+id/txtView"
                style="@style/textview_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight=".99"
                android:gravity="center"
                android:text="@string/order_popup_header"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/btn_close_popup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight=".01"
                android:src="@drawable/popup_cancel" />
        </TableRow>

        <View
            android:id="@+id/sku_search_bottom_seperator"
            style="@style/seperator_style"
            android:layout_height="1dp"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp" />
    </TableLayout>

    <!-- Displaying the Tax Wise Amount -->

<!--     <RelativeLayout -->
<!--         android:layout_width="fill_parent" -->
<!--         android:layout_height="wrap_content" > -->

        <!-- creating Horizontal scroll for the element -->

<!--         <HorizontalScrollView -->
<!--             android:id="@+id/billHorizontalScrollView" -->
<!--             android:layout_width="fill_parent" -->
<!--             android:layout_height="wrap_content" > -->

            <ScrollView
                android:id="@+id/orderScrollView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <TableLayout
                    android:id="@+id/orderSummaryPopupTable"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2dp"
                    android:orientation="vertical" 
                    android:weightSum="100">

                    <!-- table header for the sku details -->
                    <TableRow
                        android:id="@+id/orderSummaryPopupRow2"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:text="@string/gross_amt_label2"
                            android:textColor="@color/highlight_text_color" />

                        <TextView
                            android:id="@+id/totalGrossAmt1"
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:layout_marginRight="1dp"
                            android:gravity="right"
                            android:text="@string/initial_gross_amt" />
                    </TableRow>
                    <TableRow
                        android:id="@+id/orderSummaryPopupRow"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:text="@string/disc_amt_label"
                            android:textColor="@color/highlight_text_color" />

                        <TextView
                            android:id="@+id/totalDiscAmt1"
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:layout_marginRight="1dp"
                            android:gravity="right"
                            android:text="@string/initial_gross_amt" />
                    </TableRow>



                    <TableRow
                        android:id="@+id/orderSummaryPopupRow3"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:text="@string/taxable_amt_label"
                            android:textColor="@color/highlight_text_color" />

                        <TextView
                            android:id="@+id/totalTaxableAmt1"
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:layout_marginRight="1dp"
                            android:gravity="right"
                            android:text="@string/initial_gross_amt" />
                    </TableRow>

                     <TableRow
                        android:id="@+id/orderSummaryPopupRow4"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:text="@string/total_tax_amt_label"
                            android:textColor="@color/highlight_text_color" />

                        <TextView
                            android:id="@+id/totalTaxAmt1"
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:layout_marginRight="1dp"
                            android:gravity="right"
                            android:text="@string/initial_gross_amt" />
                    </TableRow>



<!--                     <TableRow -->
<!--                         android:id="@+id/orderSummaryPopupRow5" -->
<!--                         android:layout_width="fill_parent" -->
<!--                         android:layout_height="wrap_content" > -->

<!--                         <TextView -->
<!--                             style="@style/textview_style" -->
<!--                             android:layout_height="wrap_content" -->
<!--                             android:layout_weight="0.5" -->
<!--                             android:text="@string/credit_note_amt_lable" -->
<!--                             android:textColor="@color/highlight_text_color" /> -->

<!--                         <TextView -->
<!--                             android:id="@+id/creditNoteAmt" -->
<!--                             style="@style/textview_style" -->
<!--                             android:layout_height="wrap_content" -->
<!--                             android:layout_weight="0.5" -->
<!--                             android:layout_marginRight="1dp" -->
<!--                             android:gravity="right" -->
<!--                             android:text="@string/initial_gross_amt" /> -->
<!--                     </TableRow> -->

                </TableLayout>
            </ScrollView>
<!--         </HorizontalScrollView> -->
<!--     </RelativeLayout> -->

    <TableLayout
        android:id="@+id/orderSummaryNetAmt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:gravity="bottom"
        android:orientation="vertical" >

        <!-- Total Bill Amount -->

        <View
            android:id="@+id/sku_search_bottom_seperator"
            style="@style/seperator_style"
            android:layout_height="1dp"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp" />

        <TableRow
            android:id="@+id/totalOrderAmountRow"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp" >

            <TextView
                android:id="@+id/totalOrderAmountText"
                style="@style/textview_style"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:text="@string/total_order_amt_label"
                android:textColor="@color/highlight_text_color"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/totalOrderAmount"
                style="@style/textview_style"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:gravity="right"
                android:text="@string/initial_gross_amt"
                android:textStyle="bold" />
        </TableRow>
        <View
            android:id="@+id/sku_search_bottom_seperator"
            style="@style/seperator_style"
            android:layout_height="1dp"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp" />
    </TableLayout>

</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接