未选中复选框且在android中单击按钮时显示Toast消息
嘿,伙计们,我有一个复选框和一个应用按钮。因此,当一些人没有选中复选框并单击应用按钮时,应该会显示一条祝酒信息,如果他选中复选框并再次单击应用按钮,则会将他带到下一个活动。这是我的密码。 maintactivity.java未选中复选框且在android中单击按钮时显示Toast消息,android,Android,嘿,伙计们,我有一个复选框和一个应用按钮。因此,当一些人没有选中复选框并单击应用按钮时,应该会显示一条祝酒信息,如果他选中复选框并再次单击应用按钮,则会将他带到下一个活动。这是我的密码。 maintactivity.java package com.meti.workforhome; import android.app.Activity; import android.content.Context; import android.content.Intent; import android
package com.meti.workforhome;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mApply = (Button) findViewById(R.id.Apply);
final CheckBox checkBox = (CheckBox) findViewById(R.id.check);
mApply.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if (checkBox.isChecked()) {
// TODO Auto-generated method stub
String clicks = "click";
String message = "Apply button clicked";
Log.i(clicks, message);
Intent i = new Intent(MainActivity.this, FormActivity.class);
startActivity(i);
}
else
{
Toast.makeText(this.getApplicationContext(), R.string.notice, Toast.LENGTH_LONG).show();
}
}
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<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=".MainActivity"
android:orientation="vertical"
android:background="#A9F5F2">
<TextView
android:id="@+id/topic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="22sp"
android:text="@string/topic" />
<ScrollView
android:id="@+id/Scrolly"
android:layout_width="275dp"
android:layout_height="200dp"
android:layout_below="@id/topic"
android:isScrollContainer="true"
android:fadingEdgeLength="14.5sp"
android:focusable="true">
<LinearLayout
android:layout_width="275dp"
android:layout_height="200dp" >
<TextView
android:id="@+id/rules"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textSize="22sp"
android:text="@string/rules" />
</LinearLayout>
</ScrollView>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Scrolly"
android:layout_below="@+id/Scrolly"
android:layout_marginTop="8dp"
android:text="@string/check" />
<Button
android:id="@+id/Apply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/check"
android:layout_below="@+id/check"
android:layout_marginTop="32dp"
android:background="#3b5998"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:text="@string/after"
android:textColor="#ffffff"
android:textStyle="bold" />
</RelativeLayout>
这个问题可能很傻。但我是android新手,请纠正我的错误。提前谢谢 删除此项:
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
其余部分可以保持原样,因为getApplicationContext()是一个系统函数,将返回应用程序的上下文。你不需要重新定义它 正如您所做的那样,它不会返回任何上下文,因为您将其设置为返回null。删除以下内容:
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
其余部分可以保持原样,因为getApplicationContext()是一个系统函数,将返回应用程序的上下文。你不需要重新定义它 正如您所做的那样,它不会返回任何上下文,因为您将其设置为返回null。删除以下内容:
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
其余部分可以保持原样,因为getApplicationContext()是一个系统函数,将返回应用程序的上下文。你不需要重新定义它 正如您所做的那样,它不会返回任何上下文,因为您将其设置为返回null。删除以下内容:
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
其余部分可以保持原样,因为getApplicationContext()是一个系统函数,将返回应用程序的上下文。你不需要重新定义它 正如您所做的,它不会返回任何上下文,因为您使它返回null。Remove
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
只用
Toast.makeText(MainActivity.this, R.string.notice, Toast.LENGTH_LONG).show();
或
您还可以使用v.getContext()
删除
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
只用
Toast.makeText(MainActivity.this, R.string.notice, Toast.LENGTH_LONG).show();
或
您还可以使用v.getContext()
删除
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
只用
Toast.makeText(MainActivity.this, R.string.notice, Toast.LENGTH_LONG).show();
或
您还可以使用v.getContext()
删除
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
只用
Toast.makeText(MainActivity.this, R.string.notice, Toast.LENGTH_LONG).show();
或
您也可以使用
v.getContext()
但问题是什么?问题是什么?问题是什么?问题是什么?