public
class
MainActivity
extends
AppCompatActivity{
private
ImageButton mSendMessage;
private
LinearLayout mMessageLog;
private
TextView text;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMessageLog = (LinearLayout)findViewById(R.id.message_log);
ExampleThread thread =
new
ExampleThread();
thread.start();
}
private
class
ExampleThread
extends
Thread {
private
static
final
String TAG =
"ExampleThread"
;
private
TextView text;
private
int
i;
public
ExampleThread() {
text =
new
TextView(MainActivity.
this
);
mMessageLog.addView(text);
}
public
void
run() {
runOnUiThread(
new
Runnable() {
@Override
public
void
run() {
while
(i<
10
)
{
try
{
text.setText(
"i="
+i);
i++;
Thread.sleep(
1000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return
true
;
}
@Override
public
boolean
onOptionsItemSelected(MenuItem item) {
int
id = item.getItemId();
if
(id == R.id.action_settings) {
return
true
;
}
return
super
.onOptionsItemSelected(item);
}