roc*_*cko 2 android button android-intent
我想创建一个按钮,单击该按钮时,它将显示另一个意图。
这是代码:
<Button
android:text="Bine ati venit!"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button1"
android:background="#479fc7"
android:textSize="40dp"
android:textColor="@color/colorPrimaryDark"
android:textStyle="normal|bold|italic"
android:textAllCaps="false"
android:fontFamily="casual"
android:onClick="next_page"/>
Run Code Online (Sandbox Code Playgroud)
以及来自 .java 类:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener( new View.OnClickListener()
{
public void onClick (View v){
next_page(v);
}
});
public void next_page(View v) {
Intent intent = new Intent(this, MapsActivity.class);
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:“无法解析符号‘setOnClickListener’”、“变量 onClick 从未使用”、“;预期”...
小智 6
// 您必须将键入的代码移至类方法中并按如下方式工作
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the View that shows the numbers category
TextView numbers = (TextView) findViewById(R.id.numbers);
// Set a click listener on that View
numbers.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
@Override
public void onClick(View view) {
Intent numbersIntent = new Intent(MainActivity.this, NumberActivity.class);
startActivity(numbersIntent);
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39369 次 |
| 最近记录: |