Android 按钮';在对TextView进行setText()调用后,s文本的对齐方式错误

Android 按钮';在对TextView进行setText()调用后,s文本的对齐方式错误,android,Android,在我的例子中,我有一个活动,带有表格布局,其中包含一些视图,例如id为“Number1”的按钮,id为“password”的文本视图 当我点击按钮时,它的事件处理程序被调用,它所做的一件事就是设置密码TextViewtext。在此之后,按钮的文本变得不对齐-而不是原来的中心对齐,按钮内的文本在底部对齐 我使用的是Android Studio 1.1 Beta 4,该项目的compileSdkVersion设置为21,minSdkVersion设置为19,targetSdkVersion设置为21

在我的例子中,我有一个
活动
,带有
表格布局
,其中包含一些视图,例如id为“Number1”的
按钮
,id为“password”的
文本视图

当我点击按钮时,它的事件处理程序被调用,它所做的一件事就是设置密码
TextView
text。在此之后,按钮的文本变得不对齐-而不是原来的中心对齐,按钮内的文本在底部对齐

我使用的是Android Studio 1.1 Beta 4,该项目的compileSdkVersion设置为21,minSdkVersion设置为19,targetSdkVersion设置为21

下面我提供了相关代码的片段

事件处理程序片段

public void buttonNumberClick(View view) {

    TextView passwordText = (TextView) findViewById(R.id.password);

    Button sourceButton = (Button)view;

    String sourceText = sourceButton.getText().toString();
    String text = passwordText.getText().toString();
    if (text.length() < 4) {
        int number = Integer.parseInt(sourceText);
        String newText = text + Integer.toString(number);

        // After this below line, the button text of the clicked button (Number1) becomes bottom aligned.
        passwordText.setText(newText);

    }
}
<TableLayout 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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <TableRow
        android:minHeight="130dp"
        android:layout_height="0dp"
        android:layout_weight="0.5">

        <LinearLayout
            android:layout_weight="0.5"
            android:layout_row="0"
            android:layout_column="1"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:orientation="vertical">

            <TextView
                android:layout_gravity="center_horizontal"
                android:id="@+id/enterPinTextView"
                android:textSize="25sp"
                android:text="Enter PIN"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_gravity="center_horizontal"
                android:id="@+id/password"
                android:textSize="25sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <RelativeLayout
                android:layout_gravity="center_horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ProgressBar
                    android:id="@+id/progressRing"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_centerInParent="true"
                    android:progressDrawable="@drawable/circular_progress_bar" />

                <TextView
                    android:text="5.0"
                    android:id="@+id/progressRingText"
                    android:textSize="20sp"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </RelativeLayout>

        </LinearLayout>

    </TableRow>

    <TableRow
        android:gravity="bottom|center"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5">

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TableRow
                android:layout_weight="0.25"
                android:layout_height="0dp"
                android:layout_width="wrap_content">

                <Button
                    android:id="@+id/buttonTest"
                    android:text="Test"
                    android:minHeight="50dp"
                    android:layout_width="72dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="0dp"
                    android:onClick="buttonTestClick" />

                <Button
                    android:id="@+id/Number1"
                    android:text="1"
                    android:minHeight="50dp"
                    android:layout_width="60dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="0dp"
                    android:onClick="buttonNumberClick" />

            </TableRow>

        </TableLayout>

    </TableRow>

</TableLayout>
public void按钮数字点击(查看){
TextView密码text=(TextView)findViewById(R.id.password);
按钮源按钮=(按钮)视图;
字符串sourceText=sourceButton.getText().toString();
String text=passwordText.getText().toString();
if(text.length()<4){
int number=Integer.parseInt(sourceText);
字符串newText=text+Integer.toString(数字);
//在下面这一行之后,单击按钮(编号1)的按钮文本变为底部对齐。
passwordText.setText(newText);
}
}
布局代码段

public void buttonNumberClick(View view) {

    TextView passwordText = (TextView) findViewById(R.id.password);

    Button sourceButton = (Button)view;

    String sourceText = sourceButton.getText().toString();
    String text = passwordText.getText().toString();
    if (text.length() < 4) {
        int number = Integer.parseInt(sourceText);
        String newText = text + Integer.toString(number);

        // After this below line, the button text of the clicked button (Number1) becomes bottom aligned.
        passwordText.setText(newText);

    }
}
<TableLayout 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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <TableRow
        android:minHeight="130dp"
        android:layout_height="0dp"
        android:layout_weight="0.5">

        <LinearLayout
            android:layout_weight="0.5"
            android:layout_row="0"
            android:layout_column="1"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:orientation="vertical">

            <TextView
                android:layout_gravity="center_horizontal"
                android:id="@+id/enterPinTextView"
                android:textSize="25sp"
                android:text="Enter PIN"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_gravity="center_horizontal"
                android:id="@+id/password"
                android:textSize="25sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <RelativeLayout
                android:layout_gravity="center_horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ProgressBar
                    android:id="@+id/progressRing"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_centerInParent="true"
                    android:progressDrawable="@drawable/circular_progress_bar" />

                <TextView
                    android:text="5.0"
                    android:id="@+id/progressRingText"
                    android:textSize="20sp"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </RelativeLayout>

        </LinearLayout>

    </TableRow>

    <TableRow
        android:gravity="bottom|center"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5">

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TableRow
                android:layout_weight="0.25"
                android:layout_height="0dp"
                android:layout_width="wrap_content">

                <Button
                    android:id="@+id/buttonTest"
                    android:text="Test"
                    android:minHeight="50dp"
                    android:layout_width="72dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="0dp"
                    android:onClick="buttonTestClick" />

                <Button
                    android:id="@+id/Number1"
                    android:text="1"
                    android:minHeight="50dp"
                    android:layout_width="60dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="0dp"
                    android:onClick="buttonNumberClick" />

            </TableRow>

        </TableLayout>

    </TableRow>

</TableLayout>

在查看了Stack Overflow like上出现的几个类似事件后,我尝试了一些不同的方法,包括以编程方式设置按钮的文本,以及再次设置按钮的重力和填充。所有这些都不起作用

我是通过反复试验得出这个解决方案的——在对setText进行有问题的调用之前和之后隐藏并显示按钮。另外,请注意,必须将“可见性”设置为“已消失”,而不是“不可见”。这可能会迫使布局引擎再次创建具有正确对齐方式的按钮

新的事件处理程序如下所示

public void buttonNumberClick(View view) {

    TextView passwordText = (TextView) findViewById(R.id.password);

    Button sourceButton = (Button)view;

    String sourceText = sourceButton.getText().toString();
    String text = passwordText.getText().toString();
    if (text.length() < 4) {
        int number = Integer.parseInt(sourceText);
        String newText = text + Integer.toString(number);

        // Fix the issue of the button text being mis-aligned after the below call to setText.
        sourceButton.setVisibility(View.GONE);

        passwordText.setText(newText);

        // Fix the issue of the button text being mis-aligned after the above call to setText.
        sourceButton.setVisibility(View.VISIBLE);
    }
}
public void按钮数字点击(查看){
TextView密码text=(TextView)findViewById(R.id.password);
按钮源按钮=(按钮)视图;
字符串sourceText=sourceButton.getText().toString();
String text=passwordText.getText().toString();
if(text.length()<4){
int number=Integer.parseInt(sourceText);
字符串newText=text+Integer.toString(数字);
//修复下面调用setText后按钮文本对齐错误的问题。
sourceButton.setVisibility(View.GONE);
passwordText.setText(newText);
//修复了上述调用setText后按钮文本对齐错误的问题。
sourceButton.setVisibility(View.VISIBLE);
}
}

。不用谢。