存储随机生成的文本视图项以与“复制到剪贴板”按钮一起使用



我在android工作室用java和xml进行编码。我已经声明了三个数组,并声明了三种TextView,它们将显示每个数组中随机选择的字符串。

我是编程新手,我需要帮助:

  • 从每个数组中挑选一个随机字符串的方法
  • 将选定字符串设置为TextViews的按钮
  • 一个按钮,用于获取TextViews中的文本并将其复制到剪贴板

到目前为止,这是我的代码:

public class MainActivity extends AppCompatActivity {
String[] listOne = new String[]{...};
String[] listTwo = new String[]{...};
String[] listThree = new String[]{...};
public TextView view1;
public TextView view2;
public TextView view3;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    view1= (TextView)findViewById(R.id.textView1);
    view2= (TextView)findViewById(R.id.textView2);
    view3= (TextView)findViewById(R.id.textView3)
    int listOneInt = new Random().nextInt(listOne.length);
    int listTwoInt = new Random().nextInt(listTwo.length);
    int listThreeInt = new Random().nextInt(listThree.length);;
}
public void generate(View view)
{
}

}

我希望这有帮助(除了复制到剪贴板的部分,对不起,我不知道)

public class MainActivity extends AppCompatActivity {
String[] listOne = new String[]{"Hello","world","123"};
String[] listTwo = new String[]{"stack","over","flow"};
String[] listThree = new String[]{"do","it","yourself"};
public TextView view1;
public TextView view2;
public TextView view3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    view1= (TextView)findViewById(R.id.textView1);
    view2= (TextView)findViewById(R.id.textView2);
    view3= (TextView)findViewById(R.id.textView3);
    Random r = new Random();
    final String randOne;
    final String randTwo;
    final String randThree;
    int listOneInt = r.nextInt(listOne.length);
    int listTwoInt = r.nextInt(listTwo.length);
    int listThreeInt = r.nextInt(listThree.length);
    randOne = listOne[listOneInt];
    randTwo = listTwo[listTwoInt ];
    randThree = listThree[listThreeInt ];
    //Assuming you have a button with id assignRand
    Button assignRandToView = (Button) findViewById(R.id.assignRand);
    assignRandToView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view1.setText(randOne);
            view2.setText(randTwo);
            view3.setText(randThree);
        }
    });
}
}

最新更新