【发布时间】:2011-05-01 19:59:14
【问题描述】:
我正在尝试使用EditText 以及当用户点击EditText 时在屏幕底部显示键盘的可能性。我知道 InputMethodService 和 SoftKeyboard 示例,但我不能以这种方式使用它,因为我的键盘应该只对 EditText 可用。
此外,应该有一个上下文菜单,但这不是这个问题的一部分(我认为)。
我已经阅读了大量代码片段,但在许多情况下,它们包含不再可用的方法(即getViewInflate()),或者是在我不理解或无法翻译成的上下文中编写的我的代码(请注意,我是 Android 新手)。
在大多数尝试中,当我点击 EditText 时,我都会因为这个异常而失败:
java.lang.IllegalArgumentException: width and height must be > 0
后面是不包含我的任何类的堆栈跟踪。正如您在下面的代码中看到的那样,所有尺寸都已设置。
您在下面看到的是代码的当前状态(我删除了一些代码,希望它仍然有意义)。我还尝试在主线程中使用 handler.post() 内部的内容,使用注释的内容而不是 handler.post() ...
下面没有尝试在一个布局 XML 中使用 RelativeLayout 和 EditText 和 KeyboardView。在创建布局时出现了一个不同的异常,例如“无效类型 0x12”或其他异常。
它只是不起作用,或者我只是不知道该怎么做。谁能指导我完成这个?如有遗漏,请告诉我。
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/field_input"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inputType="textMultiLine|textImeMultiLine"
android:typeface="monospace"
android:gravity="top|left"
android:maxLength="255"
/>
</LinearLayout>
keyboard.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.messenger.keyboard.LatinKeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
LatinKeyboardView.java:
import android.inputmethodservice.KeyboardView;
public class LatinKeyboardView extends KeyboardView {
:
}
EditorActivity.java
import android.app.Activity;
public class EditorActivity extends Activity {
private View keyboardLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
final EditText inputField;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
keyboardLayout = (View)getLayoutInflater().inflate(R.layout.keyboard, null, false);
inputField = (EditText)findViewById(R.id.field_input);
registerForContextMenu(inputField);
inputField.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.input, null, false), 100, 100, true);
PopupWindow pw = new PopupWindow(keyboardLayout, 100, 100, true);
pw.showAtLocation(findViewById(R.id.field_input), Gravity.CENTER, 0, 0);
}
});
/*
if (keyboardLayout.getVisibility() == View.GONE) {
// Show Media Player
TranslateAnimation mAnimUp =
new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, -keyboardLayout.getHeight(),
Animation.RELATIVE_TO_SELF, 0);
mAnimUp.setStartOffset(500);
mAnimUp.setDuration(500);
keyboardLayout.setVisibility(View.VISIBLE);
keyboardLayout.setAnimation(mAnimUp);
}
*/
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
:
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
:
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
:
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
:
}
}
【问题讨论】:
标签: android popupwindow