尝试使用ToggleButton在密码(*)和纯文本EditText之间循环。使用“一切”,按下切换开关,然后setInputType()运行应用程序,强制关闭任何帮助下方的代码,因为它看起来正确,因此非常有用。
setContentView(R.layout.text);
Button chkCMD = (Button) findViewById(R.id.bResults);
final ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
final EditText input = (EditText) findViewById(R.id.etCommand);
final TextView display = (TextView) findViewById(R.id.tvResults);
passTog.setOnClickListener(new View.OnClickListener(){
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);
}
}
});
};
Logcat
08-27 02:33:57.673: WARN/System.err(539): java.lang.NullPointerException
08-27 02:33:57.673: WARN/System.err(539): at com.don.hatland.TextPlay$1.onClick(TextPlay.java:38)
08-27 02:33:57.683: WARN/System.err(539): at android.view.View.performClick(View.java:2485)
08-27 02:33:57.683: WARN/System.err(539): at android.widget.CompoundButton.performClick(CompoundButton.java:99)
08-27 02:33:57.683: WARN/System.err(539): at android.view.View$PerformClick.run(View.java:9080)
08-27 02:33:57.683: WARN/System.err(539): at android.os.Handler.handleCallback(Handler.java:587)
08-27 02:33:57.683: WARN/System.err(539): at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 02:33:57.683: WARN/System.err(539): at android.os.Looper.loop(Looper.java:123)
08-27 02:33:57.683: WARN/System.err(539): at android.app.ActivityThread.main(ActivityThread.java:3647)
08-27 02:33:57.683: WARN/System.err(539): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 02:33:57.683: WARN/System.err(539): at java.lang.reflect.Method.invoke(Method.java:507)
08-27 02:33:57.683: WARN/System.err(539): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-27 02:33:57.693: WARN/System.err(539): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-27 02:33:57.693: WARN/System.err(539): at dalvik.system.NativeStart.main(Native Method)
Text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="25dp"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android.id="@+id/etCommand"
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:text="Button"
android:id="@+id/bResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"> </Button>
<ToggleButton android:layout_weight="80"
android:paddingBottom="10dp"
android:text="Try Command"
android:checked="true"
android:id="@+id/tbPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"> </ToggleButton>
</LinearLayout>
<TextView android:text="invaild" android:id="@+id/tvResults"
android:gravity="center" android:layout_width="fill_parent"
android:layout_height="wrap_content"> </TextView>
</LinearLayout>
最佳答案
您有一个小错误:
android.id="@+id/etCommand"
应该
android:id="@+id/etCommand"
^
导致
input
为空,因为在findViewById
中找不到它。关于java - setInputType()导致App中强制关闭,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7212180/