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

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);
		}
	});
  }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值