如何在Android Studio

时间:2015-08-04 06:28:15

标签: android dialog android-edittext popup android-alertdialog

如果长按PopUp Window,用户可以输入或粘贴内容,而不是按OK,然后将输入的文字放在EditText中,我就不会打开EditText ; t希望用户使用回车键。

我的主要动机是用户可以一次看到整个文本,因为我的apk中EditText大小很小。

注意: - 我是android编程的初学者,这是我的第一个问题。 :)

4 个答案:

答案 0 :(得分:2)

欢迎来到SO。在SO中提出问题之前,你必须做一些研究。请提供相关信息,这将有助于我们找到答案

作为对您的问题的回答,您可以使用setOnclickListener将onclick列表器附加到EditText,以便在用户单击EitText时触发事件。

用于包含EditText的弹出窗口,用户可以在其中输入输入

您可以使用具有自己的xml布局的对话框

查看此page以创建和使用对话框

答案 1 :(得分:1)

我对你的问题的理解。  我的回答是

  • 您可以长按“编辑文字”打开弹出窗口。
  • 将您的编辑文本文本复制到弹出窗口 - 编辑文本。
  • 按下" ok"将弹出窗口编辑文本的文本复制回基本文本框。

附上供参考的代码

弹出窗口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>

主要活动 -

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;

}

}

答案 2 :(得分:0)

大多数东西都可以在Android API页面上找到,就像Dev所说的那样。但是,这是一个从文档中略微编辑的示例,可帮助您开始使用。

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。我没有遵守此代码,因此可能存在错误,但这是概念

答案 3 :(得分: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>