چطور میشه خروجی عددی از کاربر بگیریم؟
سوال
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class MainActivity extends AppCompatActivity { Button btn; TextView txthello; TextView txtshow; EditText edtNum; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { btn = findViewById(R.id.btn); txthello = findViewById(R.id.txthello); txtshow = findViewById(R.id.txtShow); edtNum = findViewById(R.id.edtNum); String num; num = edtNum.getText().toString(); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { txthello.setText(num); } }); } catch (Exception e) { txthello.setText("error"+e); } }} این run میکنم عدد وارد میکنم دکمه میزنم هیچی تایپ نمیشه ! |
در حال بررسی
0
انجمن برنامه نویسان اندروید
3 سال
1 پاسخ
285 دیده شده
0
پاسخ ( ۱ )
منظورت اینه که کاربر یک عدد وارد کنه بعد همون رو توی textview نمایش بدی درسته؟
خب چون ورودی عدد میگیری num رو بجای اینکه String بذاری int بذار
خط ۱۷ رو هم اینطوری بنویس که ورودی به int تبدیل بشه و در num ذخیره بشه
num= Integer.parseInt(edtNum.getText().toString());
و برای چاپ کردنش هم خط زیر رو باید بنویسی که num تبدیل بشه به String
txthello.setText(num+””);