Advertisement
GerONSo

Untitled

May 29th, 2021
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1.  
  2. class SearchFragment : Fragment() {
  3.  
  4. private lateinit var searchText: EditText?
  5. private lateinit var searchResultView: TextView?
  6. private lateinit var searchButton: Button?
  7.  
  8. override fun onCreateView(
  9. inflater: LayoutInflater,
  10. container: ViewGroup?,
  11. savedInstanceState: Bundle?
  12. ): View = inflater.inflate(R.layout.fragment_search, container, false)
  13.  
  14. override fun onViewCreated(view: View, savedInstanceState: Bundle?) = runBlocking {
  15. searchText = view.findViewById(R.id.search_text)
  16. searchResultView = view.findViewById(R.id.search_result)
  17. searchButton = view.findViewById(R.id.search_button)
  18. searchButton.setOnClickListener {
  19. searchButton?.enabled = false
  20. val searchString = searchText.text.toString()
  21. launch {
  22. post(searchString)
  23. }
  24. }
  25. }
  26.  
  27. suspend fun post(searchString: String) {
  28. try {
  29. val searchResult = executeSearchRequest(searchString)
  30. } catch(e: NetworkException) {
  31. // todo
  32. }
  33. runOnUiThread {
  34. searchResultView?.text = searchResult
  35. searchButton?.enabled = true
  36. }
  37. }
  38.  
  39. override fun onSaveInstanceState(bundle: Bundle) {
  40. bundle.putExtra("searchResultViewText", searchResultView?.text)
  41. //todo
  42. }
  43.  
  44. override fun onRestoreInstanceState() {
  45. //todo
  46. }
  47.  
  48. override fun onDestroyView() {
  49. searchButton.setOnClickListener(null)
  50. searchText = null
  51. searchResultView = null
  52. searchButton = null
  53. }
  54.  
  55. private fun executeSearchRequest(searchString: String): String {
  56. // Долгая операция, возможно поход в сеть
  57. }
  58. }
  59.  
  60.  
  61. ////////////////////////////////////////
  62.  
  63. public class VV extends View {
  64.  
  65. Paint p = new Paint();
  66. Rect firstSquare;
  67. Rect secondSquare;
  68. Rect thirdSquare;
  69.  
  70. public VV(Context context) {
  71. super(context);
  72. p.setColor(Color.BLUE);
  73. }
  74.  
  75. @Override
  76. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  77. firstSquare = new Rect(0, 0, getSize(widthMeasureSpec), getSize(heightMeasureSpec));
  78. secondSquare = new Rect(100, 100, getSize(widthMeasureSpec) - 100, getSize(heightMeasureSpec) - 100);
  79. thirdSquare = new Rect(200, 200, getSize(widthMeasureSpec) - 200, getSize(heightMeasureSpec) - 200);
  80. }
  81.  
  82. private int getSize(int measureSpec) {
  83. //todo
  84. }
  85.  
  86. @Override
  87. protected void onDraw(Canvas canvas) {
  88. super.onDraw(canvas);
  89. canvas.drawRect(firstSquare, p);
  90. canvas.drawRect(secondSquare, p);
  91. canvas.drawRect(thirdSquare, p);
  92. }
  93. }
  94.  
  95. ///////////////////////////////////////
  96.  
  97. class Counter(var count: Long) {
  98. fun increment() {
  99. count++
  100. }
  101.  
  102. fun decrement() {
  103. count--
  104. }
  105. }
  106.  
  107. val counter = Counter(0)
  108.  
  109. val runnable1 = Runnable {
  110. for (i in 0..999) {
  111. counter.increment()
  112. }
  113. }
  114.  
  115. val runnable2 = Runnable {
  116. for (i in 0..999) {
  117. counter.decrement()
  118. }
  119. }
  120.  
  121. val executor = Executors.newCachedThreadPool()
  122. executor.execute(runnable1)
  123. executor.execute(runnable2)
  124.  
  125. // Каким будет значение counter.count?
  126.  
  127.  
  128.  
  129. ////////////////////////////////////////
  130.  
  131. class Task(val id: Long, val name: String)
  132.  
  133. val task1 = Task(1, "Задача")
  134. val task2 = Task(1, "Задача")
  135. val tasks = HashSet<Task>()
  136.  
  137. tasks.add(task1)
  138. tasks.add(task2)
  139.  
  140. // Каким будет содержимое tasks? Объясните причины
  141.  
  142.  
  143. ////////////////////////////////////////
  144.  
  145. public class A {
  146. private final int x;
  147.  
  148. public A(int x) {
  149. this.x = x;
  150. }
  151.  
  152. public void doIt(A a) {
  153. System.out.println(a.x);
  154. }
  155. }
  156.  
  157. public class Runner {
  158. public static void main(String[] args) {
  159. A a = new A(1);
  160. A b = new A(2);
  161. b.doIt(a);
  162. }
  163. }
  164.  
  165. 1. Скомпилируется ли такой код;
  166. 2. Будет ли работать
  167. 3. Что выведется на экран
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement