大家好,我是 android 新手,我想以下列方式自定义 android 键盘。我想在android的默认键盘上方三个按钮上一个,下一个和完成。我们可以以这种方式自定义android的默认键盘吗?
提前致谢
我的照片:
经过这么多的谷歌搜索并在stackoverflow上找到各种问题后,我得到了我想要的东西,并且我发布了我创建的所有东西。
我的活动课
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>