I have very strange problem with EditText!
I use EditText that user can enter his number and i want to get and show it on Toast/in another Activity. but finally Toast shows nothing.this is my code. how can I solve it?
my java code:
public class Step2Fragment extends Fragment {
private EditText editText;
public static String times;
private Button start;
public Step2Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_step2, container, false);
EditText editText = (EditText) view.findViewById(R.id.getNum);
times= editText.getText().toString();
start= (Button) view.findViewById(R.id.startCounting);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(),times,Toast.LENGTH_LONG).show();
Intent intent=new Intent(getContext(), CountActivity.class);
//intent.putExtra("REPEATS",number);
startActivity(intent);
}
});
return view;
}
and my xml code:
<FrameLayout
...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/texts"
android:text="enter number please"
android:gravity="center"
android:layout_marginBottom="15dp"/>
<EditText
android:layout_width="100dp"
android:layout_height="50dp"
android:id="@+id/getNum"
android:inputType="number"/>
<Button
android:layout_width="75dp"
android:layout_height="45dp"
android:id="@+id/startCounting"
android:layout_marginTop="20dp"
android:layout_gravity="center"/>
</LinearLayout>
</FrameLayout>
times= editText.getText().toString();
use this line inside onClick
method.
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
times= editText.getText().toString();
Toast.makeText(getContext(),times,Toast.LENGTH_LONG).show();
Intent intent=new Intent(getContext(), CountActivity.class);
//intent.putExtra("REPEATS",number);
startActivity(intent);
}
});
You are assigning blank content to times
because that time EditText
is blank. You have to get the content when user click the button.