您的位置:首页 > 移动开发 > Android开发

Android中显示输入的隐藏密码/Android多语系支持

2013-02-01 20:36 555 查看
1.我们常常会看到我们输入的密码都是以小黑点的形式出现,这在Android中实现是很简单的,只需要设置一个属性即可。

需要设置EditText的inputType属性,设置如下:

android:inputType="textPassword"


通常我们需要检查自己输入的密码是否出错,这时我们需要显示我们输入的内容,当用户点击一个按钮后,密码就会显示出来,实现这个需要EditText的setTransformationMethod方法。

下面实现具体的实例:

下面是实现的截图,当用户点击显示密码复选框之后,显示用户输入的隐藏内容。





下面是具体的实现代码

public class EX03_22 extends Activity
{
  private EditText et;
  private CheckBox cb;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    /* 加载main.xml Layout */
    setContentView(R.layout.main);
    /* 北findViewById()取得对象 */
    et=(EditText)findViewById(R.id.mPassword);
    cb=(CheckBox)findViewById(R.id.mCheck);
    
    /* 设定CheckBox的OnCheckedChangeListener */
    cb.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
    {
      @Override
      public void onCheckedChanged(CompoundButton arg0, boolean arg1)
      {
        if(cb.isChecked())
        {
          /* 设定EditText的内容为?见的 */
          et.setTransformationMethod(
              HideReturnsTransformationMethod.getInstance());
        }
        else
        {
          /* 设定EditText的内容为隐藏的 */
          et.setTransformationMethod(
              PasswordTransformationMethod.getInstance());
        }
      }
    });
  }
}


2.国际化日益普遍,作为应用程序我们也需要实现它的国际化,当用户选择不同的语言时,显示不同的字体,这是很基本的功能,下面的这个例子就是实现当语系发生变化时,内容也发生变化。

首先,我们设置的语系是--繁体中文,当用户点击按钮后,设置语系为日本语,这时改变相应的内容。

注:设置语系的实现代码如下:

Resources resources=getResources();
			Configuration conf=resources.getConfiguration();
			conf.locale=Locale.JAPAN;
			DisplayMetrics disMetrics=resources.getDisplayMetrics();
			resources.updateConfiguration(conf, disMetrics);


实现的截图如下:



当用户点击按钮之后,显示的画面如下:



这个例子需要先设定strings.xml文件。

设定的格式如下:



系统会自动识别需要用哪一个strings.xml文件作为显示内容。

具体的实现代码如下:

public class EX03_23 extends Activity
{
private Button button;
private TextView textView;
private TextView textView2;
private TextView textView3;
private ImageView imageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
textView=(TextView)findViewById(R.id.textview1);
textView2=(TextView)findViewById(R.id.textview2);
textView3=(TextView)findViewById(R.id.textview3);
imageView=(ImageView)findViewById(R.id.imageview);

button.setOnClickListener(new OnClickListener() {

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

Resources resources=getResources(); Configuration conf=resources.getConfiguration(); conf.locale=Locale.JAPAN; DisplayMetrics disMetrics=resources.getDisplayMetrics(); resources.updateConfiguration(conf, disMetrics);
//重新设置图标
imageView.setImageResource(R.drawable.flag);

//重新设置字符串的内容
String mess1 = getResources().getString(R.string.str1);
textView.setText(mess1);

String mess2 = getResources().getString(R.string.str2);
textView2.setText(mess2);

String mess3 = getResources().getString(R.string.str3);
textView3.setText(mess3);
}
});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: