我尝试收听ToogleButton并在onCreate方法中写入toogleButton.setOnClickListener,如下所示,但是当我点击toogle按钮时不会调用该方法
请让我知道出了什么问题..
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the EditText and Toggle buttons
final ToggleButton toggleBttn = (ToggleButton)findViewById(R.id.tggle);
final EditText editText = (EditText)findViewById(R.id.editText1);
// listen to toggleButton click
toggleBttn.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
if(toggleBttn.isChecked())
{
editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
System.out.println(" Its On");
}
else
{
editText.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL);
System.out.println(" Its Off ");
}
}
});
}
以下是活动XML
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:hint="Enter Password" >
<requestFocus />
</EditText>
<ToggleButton
android:id="@+id/tggle"
android:layout_below="@+id/editText1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="true"
/>
更新代码
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the EditText and Toggle buttons
final ToggleButton toggleBttn = (ToggleButton)findViewById(R.id.tggle);
final EditText editText = (EditText)findViewById(R.id.editText1);
// listen to toggleButton click
toggleBttn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
System.out.println(" Its On");
}
else
{
editText.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL);
System.out.println(" Its Off ");
}
}
});
答案 0 :(得分:2)
您在此处使用了错误的侦听器,请使用OnCheckedChangeListener
就像这个例子......
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled
}
}
});
您可以在http://developer.android.com/guide/topics/ui/controls/togglebutton.html
处了解更多信息我正在做这件事并且正在努力。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText e = (EditText) findViewById(R.id.type);
ToggleButton t = (ToggleButton) findViewById(R.id.tggle);
t.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "is checked", Toast.LENGTH_LONG).show();
e.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
e.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
else
{
Toast.makeText(MainActivity.this, "is un-checked", Toast.LENGTH_LONG).show();
e.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL);
}
}
});