编写Android应用程序代码:请设计一个“掌上驴友”的软件,其中登录界面上密码输入部分要求密码显示。登陆后的中心框架界面用选项卡的形式来设计,“掌上驴友”的功能分为四个模块。
时间: 2024-02-26 14:54:55 浏览: 77
用户登陆界面设计,包括用户名及密码的输入
5星 · 资源好评率100%
好的,以下是一个简单的Android应用程序代码示例,实现了“掌上驴友”软件的基本功能。请注意,此代码只是示例,您需要根据自己的需求进行修改和扩展。
```java
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
// 添加四个模块页面
viewPagerAdapter.addFragment(new Module1Fragment(), "模块1");
viewPagerAdapter.addFragment(new Module2Fragment(), "模块2");
viewPagerAdapter.addFragment(new Module3Fragment(), "模块3");
viewPagerAdapter.addFragment(new Module4Fragment(), "模块4");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
// 在登录界面上密码输入部分要求密码显示
PasswordEditText passwordEditText = (PasswordEditText) findViewById(R.id.password_edit_text);
passwordEditText.setTransformationMethod(new PasswordTransformationMethod());
}
}
```
其中,`Module1Fragment`、`Module2Fragment`、`Module3Fragment`和`Module4Fragment`是四个模块的页面。您需要根据自己的需求编写这些页面的代码。
另外,`PasswordEditText`是一个自定义的带密码显示功能的EditText控件,您需要自己实现这个控件。以下是一个简单的示例代码:
```java
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
public class PasswordEditText extends EditText {
private Drawable showPasswordDrawable;
private Drawable hidePasswordDrawable;
private boolean isPasswordVisible = false;
public PasswordEditText(Context context) {
super(context);
init();
}
public PasswordEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PasswordEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
// 获取显示密码和隐藏密码的drawable
showPasswordDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_show_password);
hidePasswordDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_hide_password);
// 设置显示密码的drawable的点击事件
showPasswordDrawable.setCallback(new Callback() {
@Override
public void invalidateDrawable(Drawable who) {
invalidate();
}
@Override
public void scheduleDrawable(Drawable who, Runnable what, long when) {
scheduleDrawable(who, what, when);
}
@Override
public void unscheduleDrawable(Drawable who, Runnable what) {
unscheduleDrawable(who, what);
}
});
showPasswordDrawable.setBounds(0, 0, showPasswordDrawable.getIntrinsicWidth(), showPasswordDrawable.getIntrinsicHeight());
// 设置隐藏密码的drawable的点击事件
hidePasswordDrawable.setCallback(new Callback() {
@Override
public void invalidateDrawable(Drawable who) {
invalidate();
}
@Override
public void scheduleDrawable(Drawable who, Runnable what, long when) {
scheduleDrawable(who, what, when);
}
@Override
public void unscheduleDrawable(Drawable who, Runnable what) {
unscheduleDrawable(who, what);
}
});
hidePasswordDrawable.setBounds(0, 0, hidePasswordDrawable.getIntrinsicWidth(), hidePasswordDrawable.getIntrinsicHeight());
// 设置EditText的输入类型为密码
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
setTransformationMethod(new PasswordTransformationMethod());
// 设置EditText的右侧drawable为显示密码的drawable
setCompoundDrawables(null, null, showPasswordDrawable, null);
// 设置EditText的drawable的点击事件
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// 判断是否点击了drawable
if (event.getX() >= getWidth() - getCompoundPaddingRight()) {
// 切换密码可见性
isPasswordVisible = !isPasswordVisible;
if (isPasswordVisible) {
setCompoundDrawables(null, null, hidePasswordDrawable, null);
setInputType(InputType.TYPE_CLASS_TEXT);
} else {
setCompoundDrawables(null, null, showPasswordDrawable, null);
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
setSelection(length());
}
}
return false;
}
});
}
}
```
阅读全文