我正在尝试制作注册页面,用户将在每个页面中输入他们喜欢的内容(activity_register_screen1
和activity_register_screen2
)。我试图使用EditText
声明findViewById
。与OnCreate
中的RegisterActivity.java
方法一样,当我在最后一页点击完成时,startRegister()
开始。运行startRegister
时,应该存储在先前页面中输入的数据,然后使用showAlertDialog
方法显示文本值。有谁知道怎么做?
这是RegisterActivity.java
public class RegisterActivity extends AppCompatActivity
{
/*UI*/
private ViewPager mViewPager;
private RegisterManager mRegisterManager;
private int[] mLayouts;
private TextView[] dots;
private LinearLayout dotsLayout;
private Button next;
private ViewPagerAdapter viewPagerAdapter;
private EditText mText;
/*Firebase*/
private DatabaseReference mDatabaseUser;
/*Other*/
public String text;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/*UI*/
/*Firebase*/
mDatabaseUser = FirebaseDatabase.getInstance().getReference().child("User");
/*Other*/
mRegisterManager = new RegisterManager(this);
if(!mRegisterManager.check())
{
mRegisterManager.setFirst(false);
Intent i = new Intent(RegisterActivity.this, GetStartedActivity.class);
startActivity(i);
finish();
}
setContentView(R.layout.activity_register);
mViewPager = (ViewPager)findViewById(R.id.activity_register_view_pager);
dotsLayout = (LinearLayout)findViewById(R.id.activity_register_layout_dot);
next = (Button)findViewById(R.id.activity_register_btn_next);
mLayouts = new int[]
{
R.layout.activity_register_screen1,
R.layout.activity_register_screen2,
R.layout.activity_register_screen3,
R.layout.activity_register_screen4,
R.layout.activity_register_screen5
};
addBottomDots(0);
viewPagerAdapter = new ViewPagerAdapter();
mViewPager.setAdapter(viewPagerAdapter);
mViewPager.addOnPageChangeListener(viewListener);
next.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int current = getItem(+1);
if(current < mLayouts.length)
{
mViewPager.setCurrentItem(current);
}
else
{
startRegister();
}
}
});
}
private void showAlertDialog(String title, String message)
{
AlertDialog alertDialog = new AlertDialog.Builder(RegisterActivity.this).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
alertDialog.show();
}
private void startRegister()
{
text = mText.getText().toString();
showAlertDialog("text", text);
}
private void addBottomDots(int position)
{
dots = new TextView[mLayouts.length];
int[] colorActive = getResources().getIntArray(R.array.dot_active);
int[] colorInactive = getResources().getIntArray(R.array.dot_inactive);
dotsLayout.removeAllViews();
for(int i = 0; i < dots.length; i++)
{
dots[i] = new TextView(this);
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(35);
dots[i].setTextColor(colorInactive[position]);
dotsLayout.addView(dots[i]);
}
if(dots.length > 0)
{
dots[position].setTextColor(colorActive[position]);
}
}
private int getItem(int i)
{
return mViewPager.getCurrentItem() + i;
}
ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener()
{
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
@Override
public void onPageSelected(int position)
{
addBottomDots(position);
if(position == mLayouts.length - 1)
{
next.setText("FINISH");
}
else
{
next.setText("NEXT");
}
}
@Override
public void onPageScrollStateChanged(int state)
{
}
};
public class ViewPagerAdapter extends PagerAdapter
{
private LayoutInflater mLayoutInflater;
@Override
public Object instantiateItem(ViewGroup container, int position)
{
mLayoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = mLayoutInflater.inflate(mLayouts[position], container, false);
mText = (EditText)v.findViewById(R.id.activity_register_screen1_text);
container.addView(v);
return v;
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object)
{
super.setPrimaryItem(container, position, object);
}
@Override
public int getCount()
{
return mLayouts.length;
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
View v = (View)object;
container.removeView(v);
}
}
}
这是RegisterManager.java
public class RegisterManager
{
SharedPreferences pref;
SharedPreferences.Editor editor;
Context context;
public RegisterManager(Context context)
{
this.context = context;
pref = context.getSharedPreferences("first", 0);
editor = pref.edit();
}
public void setFirst(boolean isFirst)
{
editor.putBoolean("check", isFirst);
editor.commit();
}
public boolean check()
{
return pref.getBoolean("check", true);
}
}
这是activity_register_screen1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/register_screen1">
<LinearLayout
android:id="@+id/activity_register_screen1_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/ic_live_help_white_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sports"
android:textColor="#ffffff"
android:textSize="40dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="The Easiest Way to Follow Your Favorite Sports!"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="24dp"/>
</LinearLayout>
<EditText
android:id="@+id/activity_register_screen1_text"
android:layout_below="@+id/activity_register_screen1_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="input!!"
android:textSize="20dp"
android:background="@drawable/input_outline"/>
</RelativeLayout>
这是activity_register_screen2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/register_screen2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/ic_live_help_white_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sports"
android:textColor="#ffffff"
android:textSize="40dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="The Easiest Way to Follow Your Favorite Sports!"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="24dp"/>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:1)
试试这个
if(mText ==null) {
mText = (EditText)v.findViewById(R.id.activity_register_screen1_text);
}
你的问题是&#34;(EditText)v.findViewById(R.id.activity_register_screen1_text)== null&#34;。因为activity_register_screen2.xml没有R.id.activity_register_screen1_text。