实际上,我正在尝试为带有或不带有物理键盘的设备制作应用程序。
我在我的第一个editText上添加了一个setOnEditorActionListener,当在物理键盘上按下actionDone或按下Enter键时,它应该使另一个editText可见并专注于它,但不会专注于它。
这是我的OnEditorActionListner方法:
public void codeText(){
code.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if(i == EditorInfo.IME_ACTION_DONE || keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if(!code.getText().toString().equals("")){
if (TextUtils.isDigitsOnly(code.getText())) {
if (code.getText().length() >= 1 && code.getText().length() <= 999999) {
qtaBox.setVisibility(View.VISIBLE);
qtaText.setVisibility(View.VISIBLE);
qta.requestFocus();
}
}
}
}
return true;
}
});
}
完整的xml代码,我要聚焦的editText是barcodeTxt我要聚焦的edittext是qtaTxt
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffffff"
tools:context=".SettingsActivity">
<android.support.v7.widget.CardView
android:id="@+id/logo"
android:layout_margin="2dp"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
app:cardBackgroundColor="#001F54"
app:cardCornerRadius="5dp"
android:layout_centerHorizontal="true"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">
<ImageButton
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="60dp"
android:id="@+id/backButton"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="@drawable/arrowback"
android:background="#00454545"
android:padding="8dp"
tools:ignore="ContentDescription" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="10dp"
android:gravity="center_vertical"
android:textAlignment="textEnd"
android:text="INVENTARIO"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText,RtlCompat" />
<ImageView
android:layout_marginEnd="10dp"
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="match_parent"
android:layout_gravity="end"
android:padding="5dp"
android:src="@drawable/storage"
tools:ignore="ContentDescription" />
</LinearLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:animateLayoutChanges="true"
android:layout_below="@id/logo"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:background="#001f54"
android:orientation="vertical">
<TextView
android:id="@+id/barcodeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginStart="3dp"
android:layout_marginBottom="2dp"
android:text="Barcode:"
android:textColor="#b1b1b1"
tools:ignore="HardcodedText" />
<android.support.v7.widget.CardView
android:id="@+id/barcodeBox"
android:layout_width="match_parent"
android:layout_height="40dp"
app:cardBackgroundColor="#01358f"
app:cardCornerRadius="5dp"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:ignore="UselessLeaf">
<ImageView
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layout_marginStart="4dp"
android:padding="5dp"
android:src="@drawable/barcodesticker"
android:tint="#ffffff"
tools:ignore="ContentDescription" />
<EditText
android:id="@+id/barcodeTxt"
android:selectAllOnFocus="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:imeOptions="actionDone"
android:inputType="number"
android:focusable="true"
tools:ignore="Autofill,LabelFor" />
</LinearLayout>
</android.support.v7.widget.CardView>
<TextView
android:id="@+id/textQuantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginTop="2dp"
android:layout_marginStart="3dp"
android:layout_marginBottom="2dp"
android:text="Quantità:"
android:textColor="#b1b1b1"
tools:ignore="HardcodedText" />
<android.support.v7.widget.CardView
android:id="@+id/qtaBox"
android:layout_width="match_parent"
android:layout_height="40dp"
app:cardBackgroundColor="#a78824"
app:cardCornerRadius="5dp"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:ignore="UselessLeaf">
<TextView
android:layout_marginStart="4dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="QTA"
android:padding="5dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:gravity="center"
android:textAlignment="center"
tools:ignore="HardcodedText" />
<EditText
android:id="@+id/qtaTxt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:imeOptions="actionDone"
android:maxLength="4"
android:selectAllOnFocus="true"
android:inputType="number"
android:focusable="true"
tools:ignore="Autofill,LabelFor" />
</LinearLayout>
</android.support.v7.widget.CardView>
<TextView
android:id="@+id/textLast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginTop="2dp"
android:layout_marginStart="3dp"
android:layout_marginBottom="2dp"
android:text="Ultimo articolo letto:"
android:textColor="#b1b1b1"
tools:ignore="HardcodedText" />
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/last"
app:cardBackgroundColor="#c800544e"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:ignore="UselessLeaf">
<ImageView
android:layout_marginStart="4dp"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:padding="5dp"
android:src="@drawable/barcodesticker"
android:tint="#ffffff"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/lastBarCode"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text=""
android:padding="5dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:gravity="center"
android:textAlignment="center"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="QTA"
android:padding="5dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:gravity="center"
android:textAlignment="center"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/lastQta"
android:layout_width="0dp"
android:layout_weight="0.3"
android:layout_height="match_parent"
android:text=""
android:padding="5dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:gravity="center"
android:textAlignment="center"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="#e3008f43"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<Button
android:id="@+id/btnConferma"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="CONFERMA"
android:background="#00001f54"
tools:ignore="HardcodedText" />
</android.support.v7.widget.CardView>
</LinearLayout>
<android.support.v7.widget.CardView
android:id="@+id/buttonBox"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_margin="2dp"
app:cardBackgroundColor="#001F54"
app:cardCornerRadius="5dp"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">
<ImageButton
android:id="@+id/itemList"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="0.2"
android:background="#00454545"
android:padding="8dp"
android:scaleType="fitCenter"
android:src="@drawable/history"
tools:ignore="ContentDescription" />
<View
android:id="@+id/divider"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:background="@drawable/gradient_separator"
tools:ignore="DuplicateIds,InefficientWeight,NestedWeights" />
<ImageButton
android:id="@+id/sendBtn"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_weight="0.2"
android:background="#00454545"
android:padding="8dp"
android:scaleType="fitCenter"
android:src="@drawable/send"
tools:ignore="ContentDescription" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
答案 0 :(得分:0)
您似乎希望将焦点集中在qta
引用而不是EditText
元素上。因此,也许首先尝试做qtaText.requestFocus()
。
如果这不能解决,则您的XML配置可能有问题。虽然不是该主题的专家,但我在最新项目中确实有一个重点突出的EditText
元素,其中XML如下所示:
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/opensans_light"
android:hint="@string/prompt_email"
android:inputType="textEmailAddress"
android:keepScreenOn="true"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/white" />
希望这可以帮助您解决问题。