导入View和Button后。我为Button创建了一个对象,在本例中是“thomasButton”。
但错误表明字段'thomasButton'没有被使用,即使我在下一行调用它。
过了一会儿,我发现如果把它放在另一个范围内就可以识别该字段。像这样,但程序仍然不会运行(启动时崩溃)。你们知道setOnLongClickListener用于按钮的正确方法是什么?
package com.example.thoma.event;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void displayMessage(View v){
TextView thomasText = (TextView)findViewById(R.id.txt_View);
thomasText.setText(R.string.rsc_Text2);x
}
Button thomasButton = (Button)findViewById(R.id.btn_Change);
// weird but I can only use Button object within an inner scope
{
thomasButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
TextView thomasText = (TextView) findViewById(R.id.txt_View);
thomasText.setText("Artificial");
// it will return false if long click wasnt long enough
// and normal click will be called
return true;
}
});
}
}
崩溃:不幸的是,App已经停止了
按钮初始化在onCreate()
检查下面的代码。 findViewById是昂贵的,所以如果你尽可能地发现这些id会更好
package com.example.thoma.event;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button thomasButton = (Button)findViewById(R.id.btn_Change);
TextView thomasText = (TextView) findViewById(R.id.txt_View);
thomasButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
thomasText.setText("Artificial");
// it will return false if long click wasnt long enough
// and normal click will be called
return true;
}
});
}
public void displayMessage(View v){
TextView thomasText = (TextView)findViewById(R.id.txt_View);
thomasText.setText(R.string.rsc_Text2);
}
}
package com.example.thoma.event;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView thomasText;
Button thomasButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thomasText = (TextView)findViewById(R.id.txt_View);
thomasButton = (Button)findViewById(R.id.btn_Change);
thomasButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
thomasText.setText("Artificial");
// it will return false if long click wasnt long enough
// and normal click will be called
return true;
}
});
}
public void displayMessage(View v){
thomasText.setText(R.string.rsc_Text2);
}
}
试试这个
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button thomasButton = (Button)findViewById(R.id.btn_Change);
thomasButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
TextView thomasText = (TextView) findViewById(R.id.txt_View);
thomasText.setText("Artificial");
return false;
}
});
}
}