我无法理解这段代码.我得到findViewById它将获得按钮小部件,然后它将投射它.然后,它将使用按钮来调用该setOnClickListener方法.但是,我不知道该传递的内容是什么setOnClickListener,我之前从未见过这样的代码.它是如何创建一个新对象但是能够在另一个方法的参数中创建自己的方法?如果有人能够解释那就好了.此外,该setOnClickListener方法采用何种类型的对象?
btn = (Button)findViewById(R.id.firstButton);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
tv.setText(months[rand.nextInt(12)]);
tv.setTextColor(Color.rgb(rand.nextInt(255)+1, rand.nextInt(255)+1, rand.nextInt(255)+1));
}
});
Run Code Online (Sandbox Code Playgroud) 可能重复:
输入时在EditText中清除文本
单击按钮时如何清除动态编辑文本的数据.我该如何编写代码?我必须使用什么功能或方法?请任何人帮助我这样做吗?
所以我有一个像这样的Android程序:
package com.example.androiddemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class AndroidDemo extends Activity {
String[] messages = {"Short Text",
"I want to show some really long text" +
"on the display of the phone. " +
"Having run out of ideas on what to type, " +
"I am adding this text which makes absolutely " +
"no sense."};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button add = (Button) findViewById(R.id.addBtn); …Run Code Online (Sandbox Code Playgroud) 我只是测试输入/输出,没有特殊用途,这是最后一个成功运行的代码:
public class MainActivity extends AppCompatActivity
{
/*
EditText username = (EditText)findViewById(R.id.editText_Username);
EditText password = (EditText)findViewById(R.id.editText_Password);
TextView inputdata = (TextView)findViewById(R.id.textView_InputData);
TextView welcome = (TextView)findViewById(R.id.textView_Welcome);
Button login=(Button)findViewById(R.id.button_Login);
Button anotherLogin=(Button)findViewById(R.id.button_Login_Another);
public void doLoginOnClick(View v)
{
String s1=username.getText().toString();
inputdata.setText(s1);
}
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
我试图捕获组件的id
findViewById(R.id.***) …