Java 单击按钮时设置Android TextView的值
在Java中,如果我想在单击Java 单击按钮时设置Android TextView的值,java,android,textview,settext,Java,Android,Textview,Settext,在Java中,如果我想在单击JButton时设置JLabel的值,我可以这样编码它 JButton button = new JButton("add"); JButton label = new JLabel(); public void addListenersToButtons(){ button.addActionListener(this); } public void actionPerformed(ActionEvent e) { Object src = e.
JButton
时设置JLabel
的值,我可以这样编码它
JButton button = new JButton("add");
JButton label = new JLabel();
public void addListenersToButtons(){
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == button) {
label.setText("this is the number = " + number);
}
}
我希望能够在Android中做类似的事情,当用户单击按钮时,我设置TextField
的值。安卓系统中的相应代码是什么
button.setOnCLickListenere(new OnCLickListsner(){
public void onClick(View v){
text.setText("text"); //assuming you have reference for the TextView and button
}
});
但是你肯定需要先学习android的基础知识。这里有一个例子:
final Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView tv = (TextView) findViewById(R.id.TextView1);
tv.setText("hellooo");
}
});
希望你能像这样做。这里有两个按钮,按
加号将增加值,按减号将减少值。互联网上必须有10k个示例…必须至少5-10分钟研究问题。
public static int num=0;
plus.setOnCLickListenere(new OnCLickListsner(){
public void onClick(View v){
num=num+1;
text.setText("Number "+num); //assuming you have reference for the TextView and button
}
});
minus.setOnCLickListenere(new OnCLickListsner(){
public void onClick(View v){
num=num-1;
text.setText("Number "+num); //assuming you have reference for the TextView and button
}
});