android - 在android中自定义键盘?

标签 android keyboard

大家好,我是 android 新手,我想通过以下方式自定义 android 键盘。我想要三个按钮 previous , next 和 done 在android的默认键盘上方。我们可以通过这种方式自定义android的默认键盘吗?

提前致谢

我的照片:enter image description here

最佳答案

经过大量谷歌搜索并在 stackoverflow 上找到各种问题后,我得到了我正在寻找的东西,并且我发布了我创建的所有东西。

我的 Activity 课

package com.example.demoappkeyboard;

import org.w3c.dom.Text;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private  EditText  ed1,ed2,ed3; 
    EditText edit3,edit4,edit5;
//  private Button sub1 , sub2 , sub3;
    private EditText[] bed = {ed1,ed2,ed3};



    private static int i = 1,j = 3 , k = 0 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


         final Dialog alertDialog = new Dialog(MainActivity.this); 
          ed1 = (EditText) findViewById(R.id.editText1);
          ed2 = (EditText) findViewById(R.id.editText2);
          ed3 = (EditText) findViewById(R.id.editText3);

          edit3 = (EditText) findViewById(R.id.edit3);



    ed1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            initiatePopupWindow("");
        }
    });

   ed2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            initiatePopupWindow("");
        }
    });

    ed3.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        initiatePopupWindow("");
    }
   });


}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    protected void initiatePopupWindow(String value) {

        final String data  ="";
        final Button sub1,sub2, sub3;
        final  EditText edit3;
        final EditText ed1,ed2,ed3;
        final RelativeLayout bottom1;
        final Dialog alertDialog = new Dialog(MainActivity.this);
        final LinearLayout mainlayout; 

        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alertDialog.getWindow().setBackgroundDrawable(
        new ColorDrawable(android.graphics.Color.TRANSPARENT));



     alertDialog.setContentView(R.layout.screen_popup);   
     edit3 = (EditText) alertDialog.findViewById(R.id.edit3);
     mainlayout = (LinearLayout)alertDialog.findViewById(R.id.popup_element);
     sub1 = (Button) alertDialog.findViewById(R.id.submit1);
     sub2 = (Button) alertDialog.findViewById(R.id.submit2);
     sub3 = (Button) alertDialog.findViewById(R.id.submit3);
     bottom1 = (RelativeLayout) alertDialog.findViewById(R.id.linearLayout3);    
     Button cancel = (Button) alertDialog.findViewById(R.id.btncancel);
     Button submitbtn = (Button) alertDialog.findViewById(R.id.btnsubmit);
     ed1=(EditText)findViewById(R.id.editText1);
     ed2=(EditText)findViewById(R.id.editText2);
     ed3=(EditText)findViewById(R.id.editText3);

     if(ed1.hasFocus())
     {
         String s1 = ed1.getText().toString();
         edit3.setText(s1); 
         edit3.setInputType(InputType.TYPE_CLASS_TEXT);
        sub1.setEnabled(true);
     }

     if(ed2.hasFocus())
     {
         String s1 = ed2.getText().toString();
         edit3.setText(s1); 
         edit3.setInputType(InputType.TYPE_CLASS_NUMBER);
     }

     if(ed3.hasFocus())
     {
         String s1 = ed3.getText().toString();
         edit3.setText(s1);
         edit3.setInputType(InputType.TYPE_CLASS_TEXT);
     }
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

        if (value.equals("")) {
                edit3.setHint("Please enter Username");
        } else {
                edit3.setText(value);
                int textLength = edit3.getText().length();
                edit3.setSelection(textLength, textLength);
        }

        alertDialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);

        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

        alertDialog.show();

        //edittext click event
        edit3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                bottom1.setVisibility(View.VISIBLE);
                sub1.setVisibility(View.VISIBLE);
                sub2.setVisibility(View.VISIBLE);
                sub3.setVisibility(View.VISIBLE);
            }
        });




       //button click event
        submitbtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                      if(ed1.hasFocus())
                          {
                              String data1 = edit3.getText().toString();
                              ed1.setText(data1); 
                              edit3.setText(data1);

                          }

                      if(ed2.hasFocus())
                          {
                              String data1 = edit3.getText().toString();
                              ed2.setText(data1); 
                              edit3.setText(data1);

                          }


                      if(ed3.hasFocus())
                          {
                              String data1 = edit3.getText().toString();
                              ed3.setText(data1); 
                              edit3.setText(data1);

                          }

                        final String value = edit3.getText().toString().toUpperCase();

//                        usern.setText(value);
                        alertDialog.cancel();
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
                }
        });
        cancel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                        alertDialog.cancel();
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
                }
        });

        sub1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if(i==1)
                 {
                  ed1.requestFocus();
                  String s1 = ed1.getText().toString();
                  edit3.setText(s1); 
                  edit3.setInputType(InputType.TYPE_CLASS_TEXT);
                  sub1.setEnabled(true);
                  sub2.setEnabled(false); 
                  //j=3;

                 }

                i++;

              if(i==2)
                {
                      ed2.requestFocus();
                      String s1 = ed2.getText().toString();
                      edit3.setText(s1); 
                      edit3.setInputType(InputType.TYPE_CLASS_NUMBER);
                        i=2;
                        sub1.setEnabled(true);
                        sub2.setEnabled(true);
                }

                if(i==3)
                {
                      ed3.requestFocus();
                      String s1 = ed3.getText().toString();
                      edit3.setText(s1);
                      edit3.setInputType(InputType.TYPE_CLASS_TEXT);
                      //i=0;
                      sub1.setEnabled(false);
                      sub2.setEnabled(true);
                      i=0;
                }

            }
        });

        sub2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                j--;
                  if(j==1) 
                  {    
                      ed1.requestFocus();
                      sub2.setEnabled(false); 
                      sub1.setEnabled(true);

                      String s1 = ed1.getText().toString();
                      edit3.setText(s1); 
                      edit3.setInputType(InputType.TYPE_CLASS_TEXT);
                       j=4;

                  }

                  if(j==2)
                  {
                      sub2.setEnabled(true);
                      sub1.setEnabled(true);
                      ed2.requestFocus();
                      String s1 = ed2.getText().toString();
                      edit3.setText(s1); 
                      edit3.setInputType(InputType.TYPE_CLASS_NUMBER);

                  }   

                  if(j==3)
                  {
                      sub2.setEnabled(true);
                      sub1.setEnabled(false);
                      ed3.requestFocus();
                      String s1 = ed3.getText().toString();
                      edit3.setText(s1);
                      edit3.setInputType(InputType.TYPE_CLASS_TEXT);                  

               }
            }
        });

    sub3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mainlayout.getWindowToken(), 0);
            bottom1.setVisibility(View.INVISIBLE);
            }
    });
    }

}

我的 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/linearLayout1"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:background="#ffffff">
        <RelativeLayout 
            android:layout_width="fill_parent" android:layout_height="50dp">

        </RelativeLayout>

        <ScrollView android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_weight="1">
            <LinearLayout android:id="@+id/linearLayout1"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:orientation="vertical" android:background="#ffffff"

                >



                     <EditText
                         android:id="@+id/editText1"
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content"
                         android:imeOptions="actionNext"
                         android:text="Name" 
                         android:maxLength="10"                       
                         android:inputType="text"
                          >
                     </EditText>

                     <EditText
                         android:id="@+id/editText2"
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content"
                         android:imeOptions="actionNext"
                         android:text="Phone" 
                         android:maxLength="10"
                         android:inputType="number"
                         />

                     <EditText
                         android:id="@+id/editText3"
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content"
                         android:layout_alignParentBottom="true"
                         android:layout_alignParentLeft="true"
                         android:layout_marginBottom="14dp"
                         android:imeOptions="actionNext"
                         android:text="Email" 
                         android:maxLength="10"
                         android:inputType="text"
                         >

                         <requestFocus />
                     </EditText>



            </LinearLayout>

        </ScrollView>
<!--for bottom bar -->
        <RelativeLayout android:layout_height="50dp"
            android:gravity="center" android:layout_width="match_parent"
            android:id="@+id/linearLayout2" android:background="#ffffff"

             >

        </RelativeLayout>

</LinearLayout>

我的 screen_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_element"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:orientation="vertical"
    android:weightSum="1" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:layout_weight=".2"
        android:gravity="bottom|center_horizontal" 
        android:orientation="vertical"
        >

        <EditText
            android:id="@+id/edit3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:maxLength="10"
            android:singleLine="true"
            android:inputType="text"
            >
            <requestFocus />
        </EditText>
        <EditText

            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:maxLength="10"
            android:singleLine="true"
            android:inputType="text"
            android:visibility="invisible"
            >

        </EditText>
        <EditText

            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:maxLength="10"
            android:singleLine="true"
            android:inputType="text"
            android:visibility="invisible"
            >

        </EditText>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight=".8"
        android:orientation="horizontal"
        android:weightSum="2" 
        android:layout_marginTop="10dp"
        >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:gravity="center" >

            <Button
                android:id="@+id/btnsubmit"
                android:layout_width="137dp"
                android:layout_height="40dp"

                android:text="Submit"
                android:textColor="#000000" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:gravity="center" >

            <Button
                android:id="@+id/btncancel"
                android:layout_width="137dp"
                android:layout_height="40dp"

                android:text="Cancel"
                android:textColor="#000000" />
        </LinearLayout>
    </LinearLayout>
<RelativeLayout android:layout_height="50dp"
            android:gravity="bottom" android:layout_width="match_parent"
            android:id="@+id/linearLayout3" android:background="#ffffff"
            android:visibility="invisible"
            >
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:weightSum="1" 
                android:orientation="horizontal" 
                 >
            <Button 
               android:id="@+id/submit1"
               android:layout_width="0dp"
               android:layout_height="wrap_content"
               android:text="Next"
               android:visibility="invisible"
               android:layout_weight=".3"
               android:layout_gravity="left" 
               > 
                </Button>
                <Button 
               android:id="@+id/submit2"
               android:layout_width="0dp"
               android:layout_height="wrap_content"
               android:text="Previous"
               android:visibility="invisible"
               android:layout_weight=".3"
               android:layout_gravity="center"
               > 
              </Button>
             <Button 
               android:id="@+id/submit3"
               android:layout_width="0dp"
               android:layout_height="wrap_content"
               android:text="Done"
               android:visibility="invisible"
               android:layout_weight=".3"
               android:layout_gravity="center"
               > 
              </Button>
        </LinearLayout>
        </RelativeLayout>
</LinearLayout>

关于android - 在android中自定义键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18887740/

相关文章:

android - 获取使用 android 的电子邮件类型和电话号码类型的标签

android - 插件com.jayway.maven.plugins.android.generation2 :android-maven-plugin:3. 1.1需要Maven版本[3.0.3,)

Python 键盘模块在没有 root 的情况下无法工作

iOS 自定义键盘扩展自动大写

android - 我如何将 Listview 标题添加到我的应用程序

android - activity.onPause 并不总是被调用

android - 在 Android 的蓝牙中,BluetoothSocket.connect 是否调用 SDP 来获取新 channel

ios - 在iOS键盘上覆盖自定义键盘

ios - React Native 中的 Keyboard.dismiss 不会关闭键盘

c++ - 在C/C++中滚动自己的键盘/输入系统