【发布时间】:2018-10-22 13:08:40
【问题描述】:
实际上我正在尝试为有或没有物理键盘的设备制作应用程序。
我在我的第一个 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>
【问题讨论】:
-
请放个断点,看看是不是进入
qta.requestFocus();这一行\ -
@UmangBurman 是的,它正在进入它,但没有集中注意力......
-
好的。你能显示xml代码吗
-
@UmangBurman 从活动中添加了完整的 xml 代码
-
在我看来,你的java代码是正确的。问题是您尝试设置焦点的
EditText与源EditText的布局不同。尝试将EditText放在一个布局中并测试您的代码。它会起作用的。
标签: android android-edittext focus