项目中遇到一个尴尬的问题,因为初学Android,所以用了大量Activity,但现在想要改成Fragment,但是Activity太多,感觉很头大,研究了很久怎么改,没有看到合适的文章,但好在最后还是成功了
这里举一个例子,免得忘记做法了:
那么就随意贴一个需要修改的Activity:
public class SetActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_set); Resources resourse = this.getResources(); String[] data = resourse.getStringArray(R.array.set); //android.R.layout.simple_list_item_1这是Android内置的布局文件,里面只有一个TextView,可用于简单显示一段文本 ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, data ); ListView listview_set = findViewById(R.id.listview_set); listview_set.setAdapter(adapter); UsersInfo user = UsersInfo.getCurrentUser(UsersInfo.class); //设置item监听事件 listview_set.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position){ case 0://修改密码 Intent intent_pwdchange = new Intent(SetActivity.this, PwdchangeActivity.class); startActivity(intent_pwdchange); break; case 1://修改注册邮箱并发送验证 Intent intent_changeemail = new Intent(SetActivity.this, ChangeemailActivity.class); startActivity(intent_changeemail); break; case 2://修改手机号码 Intent intent_changephone = new Intent(SetActivity.this, ChangephoneActivity.class