gpt4 book ai didi

android - 设置重力不起作用

转载 作者:行者123 更新时间:2023-11-29 15:29:22 25 4
gpt4 key购买 nike

请看下面我的代码。我无法使 SetGravity 工作。

怎么会?

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="25dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:longClickable="true"
android:orientation="vertical" >

<EditText
android:id="@+id/etCommands"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Type a command"
android:password="true" />


<LinearLayout
android:weightSum="100"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:layout_weight="20"
android:id="@+id/btnResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Results" />

<ToggleButton
android:paddingBottom="10dp"
android:layout_weight="80"
android:id="@+id/passTog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ToggleButton" android:checked="true"/>

</LinearLayout>

<TextView
android:id="@+id/tvResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="invalid" />

</LinearLayout>

java代码:

public class TextPlay extends Activity {

private static final String TAG = "MyApp";

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.text);

Button chkCmd = (Button) findViewById(R.id.btnResults);
final ToggleButton passTog = (ToggleButton) findViewById(R.id.passTog);
final EditText input = (EditText) findViewById(R.id.etCommands);
final TextView display = (TextView) findViewById(R.id.tvResults);

passTog.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

// TODO Auto-generated method stub

if (passTog.isChecked()) {

input.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);

} else {

input.setInputType(InputType.TYPE_CLASS_TEXT);

}

}
});

chkCmd.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String check = input.getText().toString();

Log.i(TAG, "Check Value is: " + check);

if (check.contentEquals("left")) {
Log.i(TAG, "I am under left ");
display.setGravity(Gravity.LEFT); //WHYW ILL YOU NOT WORK SETGRAVITY?????????????
} else if (check.contentEquals("center")) {
display.setGravity(Gravity.CENTER);

} else if (check.contentEquals("right")) {
display.setGravity(Gravity.RIGHT);
}

else if (check.contentEquals("blue")) {
display.setGravity(Gravity.RIGHT);
}

}
});
}
}

最佳答案

我刚刚测试过,它有效。你可能做错了几件事;

首先,当您在文本字段中键入“left”时,您是否确定没有键入“Left”(大多数键盘将第一个字母大写)

如果您确定以上内容,您是否看到“我在左下方”(而不是“检查是”)的日志?如果没有,那么它不会以某种方式到达那里......

如果您确实看到它,您可能想要清理/构建您的项目(在 Eclipse Project-> Clean - 它会清理和重建)

如果这两者都不起作用,您可能需要更改目标构建(将其更改为 API 级别 10 左右)如果您只是将其用作练习,则不必真正担心目标构建学习。

关于android - 设置重力不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8568007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com