【发布时间】:2018-06-20 18:42:19
【问题描述】:
目标:
按下按钮后,文字应实时变为文字“TEST”。
问题:
根据链接 (Change button text and action - android development) 中的代码和建议,它不会实时更改文本。您必须按两次才能更改按钮的文本。
信息:
*我是 Android 新手
谢谢!
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="test1"
android:text="Button"
tools:layout_editor_absoluteX="64dp"
tools:layout_editor_absoluteY="157dp" />
package com.jfdimarzio.test1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void test1(View v)
{
Toast.makeText(getApplicationContext(), "Test 1", Toast.LENGTH_LONG).show();
final Button btn = (Button) findViewById(R.id.btnNew);
int count = 1;
btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
test2(v);
btn.setText("test");
}
});
}
public void test2()
{
Toast.makeText(getApplicationContext(), "Test 2", Toast.LENGTH_LONG).show();
}
public void test2(View v)
{
Toast.makeText(getApplicationContext(), "Test 22", Toast.LENGTH_LONG).show();
}
}
【问题讨论】:
标签: android