我制作了一个小程序,其中我使用了一个按钮和一个WebView。 WebView可见性设置为GONE
当我按下按钮第一次我想将可见性设置为可见时,当我按下第二次按钮时,我希望可见性为GONE
。它应该连续做同样的事情。我试图使用if ..else
和switch
使其工作。奇怪的是,如果你多次点击按钮(取决于它可能是3或7或9甚至更多次)代码开始工作。
请帮帮我。
这是我的代码:
public class MyMenu extends Activity {
Button info;
WebView webView;
int see=0 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_menu);
info = (Button) findViewById(R.id.info);
webView = (WebView) findViewById(R.id.webView1);
webView.setVisibility(View.GONE);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("file:///android_asset/odigies.html");
// make listener for odigies button
info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (see == 0) {
webView.setVisibility(View.VISIBLE);
see = 1;
} else {
webView.setVisibility(View.GONE);
see = 0;
}
}
});
}
//send app with sms
public void send(View v){
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "Δωρεάν καλή εφαρμογή για Λοττο,τζοκερ,κινο και προτο.https://play.google.com/store/apps/details?id=o.tzogadoros");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
}
//send app with email
public void email(View v){
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.fromParts("mailto",
"", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Εφαρμογή Lotto Android");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Δωρεάν καλή εφαρμογή για Λοττο,τζοκερ,κινο και προτο.Δίνει τυχαίους αριθμούς για τα τυχαιρά παιχνίδια. ");
if (emailIntent.resolveActivity(getPackageManager()) == null) {
Toast.makeText(getApplicationContext(),
"Παρακαλώ παραμετροποίησε τον λογοριασμό email σου", Toast.LENGTH_LONG)
.show();
} else {
// Secondly, use a chooser will gracefully handle 0,1,2+ matching
// activities
startActivity(Intent.createChooser(emailIntent,
"Διάλεξε το email σου"));
}
}
// go to tzoker activity
public void tzoker(final View view) {
startActivity(new Intent(this, Tzoker.class));
}
// go to kino activity
public void kino(final View view) {
startActivity(new Intent(this, Kino.class));
}
// go to lotto activity
public void lotto(final View view) {
startActivity(new Intent(this, MainActivity.class));
}
// go to proto activity
public void proto(final View view) {
startActivity(new Intent(this, Proto.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
}
和xml文件:
<LinearLayout 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:background="#eaf39b"
android:orientation="vertical"
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=".MyMenu" >
<ScrollView
android:id="@+id/vertical_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<HorizontalScrollView
android:id="@+id/horizontal_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#eaf39b"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#eaf39b"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="lotto"
android:src="@drawable/lottoicon" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="tzoker"
android:src="@drawable/tzokericon" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="kino"
android:src="@drawable/kinoicon" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="proto"
android:src="@drawable/protoicon" />
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/menuicon" />
</LinearLayout>
<Button
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="@string/Menuodigies"
android:textSize="22sp"
android:textStyle="bold" />
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/sendsms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:onClick="send"
android:text="@string/sms"
android:textSize="22sp"
android:textStyle="bold" />
<Button
android:id="@+id/sendemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:onClick="email"
android:text="@string/email"
android:textSize="22sp"
android:textStyle="bold" />
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
最后当webview出现时没有缩放控件,为什么? 谢谢你的时间。
答案 0 :(得分:3)
这样更好吗?
@Override
public void onClick(View v) {
if (webView.getVisibility==View.GONE) {
webView.setVisibility(View.VISIBLE);
} else {
webView.setVisibility(View.GONE);
}
}