我想更改按钮点击textView
上的文字。活动中可以有多个按钮。主要目的是在按钮单击时更改水果的名称,或者单击任何一个按钮。
例如:单击按钮1更改名称,单击按钮2下一个名称,单击按钮3下一个名称,依此类推。我正在尝试如下,但名字没有改变。
主要活动
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 {
private TextView sentence_etx;
private Button btn_1,btn_2;
private String[] names = { "Apple","Mango","Cherry","Banana","Extra"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sentence_etx = (TextView) findViewById(R.id.etx_sentence);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
for (int i=0;i<=names.length;i++) {
work(i);
}
}
private void work(final int i) {
btn_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sentence_etx.setText(names[i]);
}
});
btn_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sentence_etx.setText(names[i]);
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.awais.test2.MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:layout_marginTop="50dp"
android:layout_marginRight="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etx_sentence"
android:text="Names......" />
<LinearLayout
android:gravity="center"
android:layout_marginTop="100dp"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<Button
android:layout_height="50dp"
android:layout_width="50dp"
android:id="@+id/btn_1"
android:text="1"/>
<Button
android:layout_height="50dp"
android:layout_width="50dp"
android:id="@+id/btn_2"
android:text="2"/>
<Button
android:layout_height="50dp"
android:layout_width="50dp"
android:id="@+id/btn_3"
android:text="3"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:1)
删除 for (int i=0;i<=names.length;i++) {
work(i);
}
,因为需要设置一次侦听器
work
所以 private void work() {
的签名将是
onCreate
并从i
将if
声明为全局并在设置下一个值时设置public class MainActivity extends AppCompatActivity {
private int i=0;
// code
@Override
protected void onCreate(Bundle savedInstanceState) {
// code
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
// initialize listener
work();
}
private void work() {
btn_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// check bounds
if (++i<names.length) {
sentence_etx.setText(names[i]);
}
// else{ // uncomment to reset the value of i
// i=0;
// }
}
});
btn_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (++i<names.length) {
sentence_etx.setText(names[i]);
}
// else{ // uncomment to reset the value of i
// i=0;
// }
}
});
}
}
条件以避免索引绑定异常
buttons
您的所有listener
正在执行相同的工作,因此您只需拥有button
并将其应用于所有public class MainActivity extends AppCompatActivity {
private TextView sentence_etx;
private Button btn_1,btn_2;
private String[] names = { "Apple","Mango","Cherry","Banana","Extra"};
private int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sentence_etx = (TextView) findViewById(R.id.etx_sentence);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
// initialize listener
btn_1.setOnClickListener(listner);
btn_2.setOnClickListener(listner);
}
OnClickListener listner=new OnClickListener() {
@Override
public void onClick(View v) {
// check bounds
if (++i<names.length) {
sentence_etx.setText(names[i]);
}
// else{ // uncomment to reset the value of i
// i=0;
// }
// note you can also add some conditional statement
// to check which button is clicked using v.getId();
}
};
}
个对象,而不是创建单独的匿名侦听器和
link.BuildUrl((SafeDictionary<string>)null)
答案 1 :(得分:0)
每次拨打OnClickListener
时,您都会重置work
按钮。如果您希望它们显示不同的名称,则无法为它们分配相同的OnClickListener
(这是您现在所做的)。
您可以做的是更改work
的签名以接收Button作为参数,并在该按钮上设置监听器。然后,在onCreate
方法中,您可以决定使用哪个按钮
答案 2 :(得分:0)
您只需要设置OnClickListener
一次
public class MainActivity extends AppCompatActivity {
private TextView sentence_etx;
private Button btn_1,btn_2;
private String[] names = { "Apple","Mango","Cherry","Banana","Extra"};
private int curIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sentence_etx = (TextView) findViewById(R.id.etx_sentence);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (curIndex == names.length - 1) {
curIndex = 0;
}
sentence_etx.setText(names[curIndex]);
curIndex++;
}
});
btn_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (curIndex == names.length - 1) {
curIndex = 0;
}
sentence_etx.setText(names[curIndex]);
curIndex++;
}
});
}
}